Format number to always show 2 decimal places

前端 未结 30 2852
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 08:17

I would like to format my numbers to always display 2 decimal places, rounding where applicable.

Examples:

number     display
------     -------
1            


        
30条回答
  •  长情又很酷
    2020-11-21 08:40

    If you're already using jQuery, you could look at using the jQuery Number Format plugin.

    The plugin can return formatted numbers as a string, you can set decimal, and thousands separators, and you can choose the number of decimals to show.

    $.number( 123, 2 ); // Returns '123.00'
    

    You can also get jQuery Number Format from GitHub.

提交回复
热议问题