Decrease the precision of the float in Javascript?

2

How to decrease the decimal places of a float number in javascript?

For example, I want 3.3333333333 to become 3.34 .

What is the simplest way to do this in javascript ?

I've tried functions like Math.ceil , but it returns me the integer value.

float_value = 3.333333;
Math.ceil(float_value); // 4
    
asked by anonymous 09.06.2015 / 19:34

2 answers

2

Use the Math.round () function returns the value of the nearest integer.

Math.round(num * 100) / 100

You can also use the toFixed () :

parseFloat("123.456").toFixed(2); // Se for uma string, converta pra numéro
    
09.06.2015 / 19:36
1

You can use toFixed () to do this, but it returns a String , not a float . From the site itself:

var num = 5.56789;
var n = num.toFixed(2);

results in

5.57

You have a discussion about this in the SO gringo .

    
09.06.2015 / 19:46