限制javascript中小数位后面显示的数字量

dot*_*tty 28 javascript floating-point decimal limit

干草,我有一些漂浮这样的

4.3455
2.768
3.67
Run Code Online (Sandbox Code Playgroud)

我想像这样展示它们

4.34
2.76
3.67
Run Code Online (Sandbox Code Playgroud)

我不想向上或向下舍入数字,只是将小数位后面显示的数字量限制为2.

T.J*_*der 49

您正在寻找toFixed:

var x = 4.3455;
alert(x.toFixed(2)); // alerts 4.35 -- not what you wanted!
Run Code Online (Sandbox Code Playgroud)

...但看起来你想要截断而不是舍入,所以:

var x = 4.3455;
x = Math.floor(x * 100) / 100;
alert(x.toFixed(2)); // alerts 4.34
Run Code Online (Sandbox Code Playgroud)


小智 11

正如TJ回答的那样,该toFixed方法将在必要时进行适当的舍入.它还会添加尾随零,这并不总是理想的.

(4.55555).toFixed(2);
//-> "4.56"

(4).toFixed(2);
//-> "4.00"
Run Code Online (Sandbox Code Playgroud)

如果将返回值强制转换为数字,那么将删除那些尾随零.这比进行自己的舍入或截断数学更简单.

+parseFloat((4.55555).toFixed(2));
//-> 4.56

+parseFloat((4).toFixed(2));
//-> 4
Run Code Online (Sandbox Code Playgroud)


NVR*_*VRM 9

大家好消息!一段时间以来,有一个替代方案:toLocaleString()

\n

虽然它并不完全适合舍入,但有一些有用的选项参数。

\n

最小整数位数

\n
\n

要使用的最小整数位数。可能的值是从 1 > 到 21;默认值为 1。

\n
\n

最小分数位数

\n
\n

要使用的最小小数位数。

\n

可能的值是从 0 > 到 20;纯数字和百分比格式的默认值为 0;t

\n

货币格式的默认值是 ISO 4217 货币代码列表提供的小单位位数(如果列表不提供该信息,则为 2)。

\n
\n

最大分数位数

\n
\n

要使用的最大小数位数。

\n

可能的值是从 0 > 到 20;纯数字格式的默认值是minimumFractionDigits和3中的较大者

\n

货币格式的默认值是minimumFractionDigits 和 ISO 4217 货币代码列表提供的小单位位数中的较大者(如果列表不提供该信息,则为 2);百分比格式的默认值是minimumFractionDigits 和0 中的较大者。

\n
\n

最小有效数字

\n
\n

要使用的最小有效数字位数。可能的值为 1 到 21;默认值为 1。

\n
\n

最大有效位数

\n
\n

要使用的最大有效数字位数。可能的值为 1 到 21;默认值为 21。

\n
\n

用法示例:

\n

\r\n
\r\n
var bigNum = 8884858284485 * 4542825114616565\nvar smallNum = 88885 / 4545114616565\n\nconsole.log(bigNum) // Output scientific\n\nconsole.log(smallNum) // Output scientific\n\n// String\nconsole.log(\n  bigNum.toLocaleString(\'fullwide\', {useGrouping:false})\n) \n\n// Return a string, rounded at 12 decimals\nconsole.log(\n  smallNum.toLocaleString(\'fullwide\', {maximumFractionDigits:12})\n)\n\n\n// Return a string, rounded at 8 decimals\nconsole.log(\n  smallNum.toLocaleString(\'fullwide\', {minimumFractionDigits:8, maximumFractionDigits:8})\n)\n\n// Return an Integer, rounded as need, js will convert it back to scientific!\nconsole.log(\n  +smallNum.toLocaleString(\'fullwide\', {maximumFractionDigits:12})\n)\n\n// Return same Integer, don\'t use parseInt for precision!\nconsole.log(\n  parseInt(smallNum.toLocaleString(\'fullwide\', {maximumFractionDigits:12}))\n)
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

但这与问题不符,它是四舍五入:

\n

\r\n
\r\n
function cutDecimals(number,decimals){\n  return number.toLocaleString(\'fullwide\', {maximumFractionDigits:decimals})\n}\n\nconsole.log(\n  cutDecimals(4.3455,2),\n  cutDecimals(2.768,2),\n  cutDecimals(3.67,2)\n)
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b

\n


And*_*y E 6

如果你不想四舍五入至小数点后2位,用toFixed()四舍五入到ñ小数和砍那些关闭,但2:

var num = 4.3455.toFixed(20);
alert(num.slice(0, -18));
//-> 4.34
Run Code Online (Sandbox Code Playgroud)

请注意,当传入的小数位数toFixed()小于传入的实际数字的小数位数且小数位数是大数时,这确实有一个小的下舍入.比如(4.99999999999).toFixed(10)会给你5.0000000000.但是,如果可以确保小数位数低于传递的小数位数,则不会出现问题toFixed().但是,它确实使@TJ的解决方案更加强大.


SC1*_*000 5

警告!目前接受的解决方案在某些情况下失败,例如4.27错误地返回4.26.

这是一个始终有效的通用解决方案.

(也许我应该把它作为评论,但在撰写本文时,我没有所需的声誉)