Format number to always show 2 decimal places

前端 未结 30 2846
爱一瞬间的悲伤
爱一瞬间的悲伤 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:29

    var number = 123456.789;
    
    
    console.log(new Intl.NumberFormat('en-IN', { maximumFractionDigits: 2 }).format(number));
    

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

提交回复
热议问题