Ton*_*bet 3 javascript number-formatting
我需要这种格式:
555.555.55,55
555.555.55,50 /* Note te extra zero */
Run Code Online (Sandbox Code Playgroud)
我这样想
new Intl.NumberFormat("es-ES").format(current.toFixed(2));
Run Code Online (Sandbox Code Playgroud)
但这打印出来了
555.555.55,5
Run Code Online (Sandbox Code Playgroud)
任何的想法?
new Intl.NumberFormat("es-ES").format(current.toFixed(2));
^ ^
Run Code Online (Sandbox Code Playgroud)
调用current.toFixed(2)将返回一个string已经有2个小数位的实例.
对NumberFormat.prototype.format字符串实例的调用将导致它将字符串转换回数字,然后根据es-ES区域性规则对其进行格式化,从而丢失有关固定小数位格式的信息.
相反,NumberFormat使用options指定的对象进行实例化minimumFractionDigits:
new Intl.NumberFormat("es-ES", { minimumFractionDigits: 2 } ).format( current );
Run Code Online (Sandbox Code Playgroud)
Intl.NumberFormat如果您要重复使用它,请记住缓存您的对象,这样您就不会每次都重新创建它.
| 归档时间: |
|
| 查看次数: |
366 次 |
| 最近记录: |