Search code examples
javascriptfloating-point

How to format a float in javascript?


In JavaScript, when converting from a float to a string, how can I get just 2 digits after the decimal point? For example, 0.34 instead of 0.3445434.


Solution

  • var result = Math.round(original*100)/100;
    

    The specifics, in case the code isn't self-explanatory.

    edit: ...or just use toFixed, as proposed by Tim Büthe. Forgot that one, thanks (and an upvote) for reminder :)