YosiFZ
YosiFZ

Reputation: 7900

How to format a float to two digit

I am using this code to get float with only two number after the . :

var number = 1.9090909090909092;
var newNumber = (number).toFixed(2);

And in newNumber i get : 1.91 instead of 1.90. Any idea why it happen?

Upvotes: 0

Views: 53

Answers (2)

cpolito
cpolito

Reputation: 76

if you want to format a float to any number of digits use this function:

function truncate(num, pos) {

    return Math.floor(num*Math.pow(10, pos))/Math.pow(10, pos);

}

where num is the float and pos is the number of digits you want after the decimal.

Upvotes: 3

Mahmoud Usama Fawzy
Mahmoud Usama Fawzy

Reputation: 11

He approximate The number

Example

25.65 = 25.7

5.699 = 5.70

Upvotes: -1

Related Questions