格式编号始终显示2位小数

编程入门 行业动态 更新时间:2024-10-23 19:29:42
本文介绍了格式编号始终显示2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

示例:

我的数字格式总是显示2位小数,

数字显示 ------ ------- 1 1.00 1.341 1.34 1.345 1.35

我一直在使用这个:

parseFloat(NUM).toFixed(2);

但是它显示 1 为 1 ,而不是 1.00 。

解决方案

这在FF4中正常工作:

现场演示 $ b

parseFloat(Math.round(num3 * 100)/ 100)。 toFixed(2);

注意,它会舍入到小数点后两位,所以输入 1.346 会返回 1.35 。

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

Examples:

number display ------ ------- 1 1.00 1.341 1.34 1.345 1.35

I have been using this:

parseFloat(num).toFixed(2);

But it's displaying 1 as 1, rather than 1.00.

解决方案

This works fine in FF4:

Live Demo

parseFloat(Math.round(num3 * 100) / 100).toFixed(2);

Note that it will round to 2 decimal places, so the input 1.346 will return 1.35.

更多推荐

格式编号始终显示2位小数

本文发布于:2023-11-10 06:18:46,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   编号   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!