Sunday, May 19, 2024
91
rated 0 times [  96] [ 5]  / answers: 1 / hits: 75977  / 14 Years ago, tue, november 23, 2010, 12:00:00

Hay, i have some floats like these



4.3455
2.768
3.67


and i want to display them like this



4.34
2.76
3.67


I don't want to round the number up or down, just limit the amount of numbers shown after the decimal place to 2.


More From » floating-point

 Answers
180

You're looking for toFixed:



var x = 4.3455;
alert(x.toFixed(2)); // alerts 4.35 -- not what you wanted!


...but it looks like you want to truncate rather than rounding, so:



var x = 4.3455;
x = Math.floor(x * 100) / 100;
alert(x.toFixed(2)); // alerts 4.34

[#94869] Saturday, November 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;