Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding a float in JavaScript

Tags:

javascript

I have a floating point number in JavaScript, like 42.563134634634. I want to display it as a string "42.56", with exactly two decimal places. How do I do that?

like image 984
Ram Rachum Avatar asked Feb 23 '23 15:02

Ram Rachum


2 Answers

Use toFixed method:

var num = 42.563134634634;
alert(num.toFixed(2));
like image 156
Igor Dymov Avatar answered Feb 26 '23 06:02

Igor Dymov


You could do

var num = 42.563134634634;
var res = num.toFixed(2);
like image 33
Aleks G Avatar answered Feb 26 '23 05:02

Aleks G