Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a float number without rounding?

Tags:

javascript

I'm trying to format a float number (lets say 341.75) by setting the decimal point to "1". I tried:

var num = 341.75;
alert(num.toFixed(1)); // output: 341.8
alert(num.toPrecision(1)); // output: 341.8

but what I need is 341.7 ! apparently both methods try to round it, is there a way to do that without rounding ?

like image 358
Tohid Avatar asked Jan 20 '12 18:01

Tohid


2 Answers

What about little cheating?

Math.floor(num * 10) / 10
like image 132
Sergio Tulentsev Avatar answered Oct 02 '22 16:10

Sergio Tulentsev


You could subtract 0.05 before formatting.

like image 25
Fantius Avatar answered Oct 02 '22 15:10

Fantius