简体   繁体   中英

float value extraction in javascript

how can i extract float value like (0.9) to be a whole integer value (9) in JavaScript?

example

var total = 67.9; 
something to process  var whole var DEC to be like 
var whole = 67;
var DEC = 9;

The simplest way I can think of is this:

var string = total.toString();
var dotidx = string.indexOf(".");
var dec = dotidx >= 0 ? +string.slice(dotidx + 1) : 0;

Notice that it doesn't work with numbers in exponential forms.

There are many ways to find a result. I give you one simple solution.

var total = 67.9
var splitVal = total.toString().split(".")
var whole = splitVal[0], var desc = splitVal[1] || 0 

and if you want to convert whole and desc in number then multiply by 1.

var whole = splitVal[0] * 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM