5

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 ?

Tohid
  • 21,535
  • 8
  • 30
  • 43
  • To format a float without rounding, you'd have to print it in it's binary form. You're converting it to a decimal representation, there will be discrepancies along the way. – Damien_The_Unbeliever Jan 20 '12 at 18:35
  • Check out this post: [http://stackoverflow.com/questions/4187146/javascript-display-two-decimal-places-no-rounding][1] [1]: http://stackoverflow.com/questions/4187146/javascript-display-two-decimal-places-no-rounding – Foggzie Jan 20 '12 at 18:35
  • `num.toStr()` to convert to a string, then substring operations to extract the chunks you want? – Marc B Jan 20 '12 at 18:38

5 Answers5

10

What about little cheating?

Math.floor(num * 10) / 10
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
6

You could subtract 0.05 before formatting.

Fantius
  • 3,806
  • 5
  • 32
  • 49
  • This one is the least involved. +1 – calebds Jan 20 '12 at 18:39
  • 3
    It stops being least involved when you make it work for all numbers. – Sergio Tulentsev Jan 20 '12 at 18:42
  • 1
    Seems to fail if you have `341.5`. The result I get is `341.4` for some reason. –  Jan 20 '12 at 18:43
  • This is the standard way to do it on programming langueses that do not support rounding down. You might want to wrap it in a function with arbitrary cuttoff point. – dtech Jan 20 '12 at 18:44
  • 1
    @am That is very strange. As Javascript uses asymetric positive infinity rounding you should get 341.5. This behaviour can happen in programming languages that use banker's rounding by default. It might be due to small floating point errors. – dtech Jan 20 '12 at 18:49
  • OK, here's the general version: you could subtract 0.5 / Math.pow(10, p) where p is the precision that you are passing to toFixed(). – Fantius Jan 20 '12 at 20:28
4

You could use the substring() method after having converted your number to a String:

num = num.substring(0, num.indexOf(".") + 2));
talnicolas
  • 13,885
  • 7
  • 36
  • 56
1

I don't see any other suitable method (atleast on the MDN reference for Number)

So maybe you could go the hackish route:

var num = 341.75;  //341; //try without decimal
var str = num.toString();
var len = str.indexOf(".")==-1 ? str.length : str.indexOf(".")+2;
alert(str.substring(0,len));

See it in action here: http://jsfiddle.net/giddygeek/YtTzL/3/

gideon
  • 19,329
  • 11
  • 72
  • 113
  • yep, +1'd it, now that's good thinking! While typing out my question (there weren't any answers) by the time I saved the fiddle and posted, I figured they'd be some smarter answers! =S – gideon Jan 20 '12 at 18:46
  • To whoever downvoted, not sure why, it may be a hacky solution, but it does work! – gideon Jan 20 '12 at 18:51
  • I didn't downvote, but you'd have an issue if there's no `.` in the original number. `.indexOf()` will return `-1` in that case, so you'll end up with `str.substring(0,1)`. –  Jan 20 '12 at 18:55
0
   You can try this if you 

function myFunction() {
            var str = 12.234556; 
            str = str.toString().split('.');
            var res = str[1].slice(0, 2);
            document.getElementById("demo").innerHTML = str[0]+'.'+res;
        }
        
       myFunction();
<div id="demo"></>

want to round

ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36