Format numbers in JavaScript similar to C#

后端 未结 18 2112
傲寒
傲寒 2020-11-22 12:26

Is there a simple way to format numbers in JavaScript, similar to the formatting methods available in C# (or VB.NET) via ToString(\"format_provider\") or

18条回答
  •  醉酒成梦
    2020-11-22 12:46

    Yes, there is definitely a way to format numbers properly in javascript, for example:

    var val=2489.8237
    
    val.toFixed(3) //returns 2489.824 (round up)
    val.toFixed(2) //returns 2489.82
    val.toFixed(7) //returns 2489.8237000 (padding)
    

    With the use of variablename.toFixed .

    And there is another function toPrecision() . For more detail you also can visit

    http://raovishal.blogspot.com/2012/01/number-format-in-javascript.html

提交回复
热议问题