Certainly a stupid question, please forgive me. My customer wants decimal numbers to display with five digits. For example: 100.34 or 37.459. I was accomplishing this with val.toPrecision (5);; however, when my numbers get really small, I stop getting what I want. For example, if my number is 0.000347, it displays 0.00034700. Now, I understand why it's doing this, but what I don't know is how to get it to display 0.0003. Any thoughts?
- 2Check out stackoverflow.com/questions/610406/… and the first answer's sprintf linkmrk– mrk2011-07-08 15:14:20 +00:00Commented Jul 8, 2011 at 15:14
Add a comment |
4 Answers
Math.round(0.000347 * 1e4) / 1e4 Or with toFixed:
Number.prototype.toNDigits = function (n) { return (Math.abs(this) < 1) ? this.toFixed(n - 1) : this.toPrecision(n); }; Comments
You can use the toFixed method to accomplish this. For example: (0.000347).toFixed(4)
5 Comments
Michael Myers
Will toFixed change 100.34298 to 100.34 as he wants?
katspaugh
jQuery for
toFixed, are you kidding?James Allardice
Why would you need to be using jQuery to use
toFixed?codeandcloud
oh pal. just leave jquery once in a while :)
Nathan
You should totally drop that and use jQuery.
The Javascript toFixed() function will truncate small numbers to a fixed decimal precision, eg:
var num = .0000350; var result = num.toFixed(5); // result will equal .00003