How to make 5509.099999999999 as 5509.09 using javascript

How to make 5509.099999999999 as 5509.09 using javascript.

+3
source share
6 answers

Lots of mathy options that end in .1, so what about:

var f = 5509.099999999999

if ((f = f.toString()).indexOf(".") >= 0)
    f = f.substr(0, 3 + f.indexOf("."))

print(parseFloat(f))

>>5509.09
+4
source

Have you tried this?

var value = 5509.099999999999;
var str = value.toString();
var result = str.substr(0,7);

Then, if you need a float again, you can do:

var FinalAnswer = parseFloat(result);

You do not need all these variables, but it is a step by step.

+2
source

, :

function truncateNumber(number, digits){
 var divisor = Math.pow(10,digits);
 return Math.floor(number*divisor)/divisor;
}

, JavaScript, Number.toFixed. , , Number.toPrecision.

0

.toFixed(2), , 5509.10 5509.09.

- Math.floor(), , . , , , 2 , 100, Math.floor(), 100.

var value = 5509.099999999999;
var result = Math.floor(value*100)/100;

[EDIT] , , - - 100 550910.

, , , .

var value = 5509.099999999999;
var str_value = value.toString();
var bits = str_value.split('.');
var result = bits[0]+"."+bits[1].substr(0,2);

, , , , , .

0

if you want to take two decimal places, you can use the .toPrecision(n)javascript function , where n is the desired number of digits total .

so for your example you will need to do

var x = 5509.099999999999;
x = x.toPrecision(6);

however, rounds lead to 5509.10

0
source
   var result = (Math.round((5509.09999 * 100) - 1)) / 100;
0
source

All Articles