Suhayb Kharabsheh Suhayb Kharabsheh - 1 year ago 101
Javascript Question

float value extraction in javascript

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


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download