Am I missing something here?
我是不是漏掉了什么?
var someNumber = 123.456;
someNumber = someNumber.toFixed(2);
alert(typeof(someNumber));
//alerts string
Why does .toFixed()
return a string?
为什么.toFixed()返回一个字符串?
I want to round the number to 2 decimal digits.
我想把这个数四舍五入到两位小数。
10 个解决方案
#1
89
It returns a string because 0.1, and powers thereof (which are used to display decimal fractions), are not representable (at least not with full accuracy) in binary floating-point systems.
它返回一个字符串,因为在二进制浮点系统中,0。1和它的幂(用于显示十进制分数)是不可表示的(至少不是完全精确)。
For example, 0.1 is really 0.1000000000000000055511151231257827021181583404541015625, and 0.01 is really 0.01000000000000000020816681711721685132943093776702880859375. (Thanks to BigDecimal
for proving my point. :-P)
例如,0。1是0。10000000000000000555111531257827021181583404541015625,0。01是0。010000000000000000208166817216851329430937702880859375。感谢BigDecimal证明了我的观点。:- p)
Therefore (absent a decimal floating point or rational number type), outputting it as a string is the only way to get it trimmed to exactly the precision required for display.
因此(没有十进制浮点数或有理数类型),输出它作为字符串是使它精确裁剪到显示所需的精度的唯一方法。
#2
93
I've solved this problem by changing this:
我通过改变这个问题来解决这个问题:
someNumber = someNumber.toFixed(2)
...to this:
…:
someNumber = +someNumber.toFixed(2);
However this will convert the number to a string and parse it again, which will have a significant impact on performance. If you care about performance or type safety, check the the other answers as well.
但是,这将把数字转换为字符串并再次解析它,这将对性能产生重大影响。如果您关心性能或类型安全,请检查其他答案。
#3
89
Number.prototype.toFixed
is a function designed to format a number before printing it out. It's from the family of toString
, toExponential
and toPrecision
.
Number.prototype。toFixed是一种设计用来格式化数字然后打印出来的函数。它来自toString家族,指数级和toPrecision。
To round a number, you would do this:
对于整数,你可以这样做:
someNumber = 42.008;
someNumber = Math.round( someNumber * 1e2 ) / 1e2;
someNumber === 42.01;
// if you need 3 digits, replace 1e2 with 1e3 etc.
.
。
Or if you want a native-like function, here you are:
如果你想要一个类似于本机的函数,你可以这样做:
Number.prototype.toFixedNumber = function(x, base){
var pow = Math.pow(base||10,x);
return +( Math.round(this*pow) / pow );
}
someNumber = 42.008;
someNumber = someNumber.toFixedNumber(2);
someNumber === 42.01;
//or even hexadecimal
someNumber = 0xAF309/256 //which is af3.09
someNumber = someNumber.toFixedNumber(1, 16);
someNumber.toString(16) === "af3.1";
.
。
Tho I don't like jsperf that much, here's a link with comparsion of the method using conversion (in other answers) and this one:
https://jsperf.com/rounding-a-number-2
我不太喜欢jsperf,这里有一个使用转换(在其他答案中)的方法的比较链接,这个链接是https://jsperf.com/round- a-number-2
#4
18
Why not use parseFloat
?
为什么不使用parseFloat吗?
var someNumber = 123.456;
someNumber = parseFloat(someNumber.toFixed(2));
alert(typeof(someNumber));
//alerts number
#5
12
Of course it returns a string. If you wanted to round the numeric variable you'd use Math.round() instead. The point of toFixed is to format the number with a fixed number of decimal places for display to the user.
当然它会返回一个字符串。如果想要对数值变量进行四舍五入,可以使用Math.round()。toFixed的要点是用一个固定的十进制位的数字格式显示给用户。
#6
11
I solved it with converting it back to number using JavaScript's Number()
function
我通过使用JavaScript的number()函数将它转换回number来解决这个问题
var x = 2.2873424;
x = Number(x.toFixed(2));
#7
3
What would you expect it to return when it's supposed to format a number ? If you have a number you can't pretty much do anything with it because e.g.2 == 2.0 == 2.00
etc. so it has to be a string.
当它要格式化一个数字时,你希望它返回什么?如果你有一个数字,你几乎不能用它来做任何事情,因为。2 == 2.0 == 2.00等等,所以它必须是一个字符串。
#8
1
Because its primary use is displaying numbers? If you want to round numbers, use Math.round()
with apropriate factors.
因为它的主要用途是显示数字?如果想要整数,可以使用Math.round()和固有因子。
#9
1
You can simply use a '+' to convert the result to a number.
您可以使用“+”将结果转换为数字。
var x = 22.032423;
x = +x.toFixed(2); // x = 22.03
#10
0
Here's a slightly more functional version of the answer m93a
provided.
下面是m93a提供的稍微更实用的答案。
const toFixedNumber = (toFixTo = 2, base = 10) => num => {
const pow = Math.pow(base, toFixTo)
return +(Math.round(num * pow) / pow)
}
const oneNumber = 10.12323223
const result1 = toFixedNumber(2)(oneNumber) // 10.12
const result2 = toFixedNumber(3)(oneNumber) // 10.123
// or using pipeline-operator
const result3 = oneNumber |> toFixedNumber(2) // 10.12
#1
89
It returns a string because 0.1, and powers thereof (which are used to display decimal fractions), are not representable (at least not with full accuracy) in binary floating-point systems.
它返回一个字符串,因为在二进制浮点系统中,0。1和它的幂(用于显示十进制分数)是不可表示的(至少不是完全精确)。
For example, 0.1 is really 0.1000000000000000055511151231257827021181583404541015625, and 0.01 is really 0.01000000000000000020816681711721685132943093776702880859375. (Thanks to BigDecimal
for proving my point. :-P)
例如,0。1是0。10000000000000000555111531257827021181583404541015625,0。01是0。010000000000000000208166817216851329430937702880859375。感谢BigDecimal证明了我的观点。:- p)
Therefore (absent a decimal floating point or rational number type), outputting it as a string is the only way to get it trimmed to exactly the precision required for display.
因此(没有十进制浮点数或有理数类型),输出它作为字符串是使它精确裁剪到显示所需的精度的唯一方法。
#2
93
I've solved this problem by changing this:
我通过改变这个问题来解决这个问题:
someNumber = someNumber.toFixed(2)
...to this:
…:
someNumber = +someNumber.toFixed(2);
However this will convert the number to a string and parse it again, which will have a significant impact on performance. If you care about performance or type safety, check the the other answers as well.
但是,这将把数字转换为字符串并再次解析它,这将对性能产生重大影响。如果您关心性能或类型安全,请检查其他答案。
#3
89
Number.prototype.toFixed
is a function designed to format a number before printing it out. It's from the family of toString
, toExponential
and toPrecision
.
Number.prototype。toFixed是一种设计用来格式化数字然后打印出来的函数。它来自toString家族,指数级和toPrecision。
To round a number, you would do this:
对于整数,你可以这样做:
someNumber = 42.008;
someNumber = Math.round( someNumber * 1e2 ) / 1e2;
someNumber === 42.01;
// if you need 3 digits, replace 1e2 with 1e3 etc.
.
。
Or if you want a native-like function, here you are:
如果你想要一个类似于本机的函数,你可以这样做:
Number.prototype.toFixedNumber = function(x, base){
var pow = Math.pow(base||10,x);
return +( Math.round(this*pow) / pow );
}
someNumber = 42.008;
someNumber = someNumber.toFixedNumber(2);
someNumber === 42.01;
//or even hexadecimal
someNumber = 0xAF309/256 //which is af3.09
someNumber = someNumber.toFixedNumber(1, 16);
someNumber.toString(16) === "af3.1";
.
。
Tho I don't like jsperf that much, here's a link with comparsion of the method using conversion (in other answers) and this one:
https://jsperf.com/rounding-a-number-2
我不太喜欢jsperf,这里有一个使用转换(在其他答案中)的方法的比较链接,这个链接是https://jsperf.com/round- a-number-2
#4
18
Why not use parseFloat
?
为什么不使用parseFloat吗?
var someNumber = 123.456;
someNumber = parseFloat(someNumber.toFixed(2));
alert(typeof(someNumber));
//alerts number
#5
12
Of course it returns a string. If you wanted to round the numeric variable you'd use Math.round() instead. The point of toFixed is to format the number with a fixed number of decimal places for display to the user.
当然它会返回一个字符串。如果想要对数值变量进行四舍五入,可以使用Math.round()。toFixed的要点是用一个固定的十进制位的数字格式显示给用户。
#6
11
I solved it with converting it back to number using JavaScript's Number()
function
我通过使用JavaScript的number()函数将它转换回number来解决这个问题
var x = 2.2873424;
x = Number(x.toFixed(2));
#7
3
What would you expect it to return when it's supposed to format a number ? If you have a number you can't pretty much do anything with it because e.g.2 == 2.0 == 2.00
etc. so it has to be a string.
当它要格式化一个数字时,你希望它返回什么?如果你有一个数字,你几乎不能用它来做任何事情,因为。2 == 2.0 == 2.00等等,所以它必须是一个字符串。
#8
1
Because its primary use is displaying numbers? If you want to round numbers, use Math.round()
with apropriate factors.
因为它的主要用途是显示数字?如果想要整数,可以使用Math.round()和固有因子。
#9
1
You can simply use a '+' to convert the result to a number.
您可以使用“+”将结果转换为数字。
var x = 22.032423;
x = +x.toFixed(2); // x = 22.03
#10
0
Here's a slightly more functional version of the answer m93a
provided.
下面是m93a提供的稍微更实用的答案。
const toFixedNumber = (toFixTo = 2, base = 10) => num => {
const pow = Math.pow(base, toFixTo)
return +(Math.round(num * pow) / pow)
}
const oneNumber = 10.12323223
const result1 = toFixedNumber(2)(oneNumber) // 10.12
const result2 = toFixedNumber(3)(oneNumber) // 10.123
// or using pipeline-operator
const result3 = oneNumber |> toFixedNumber(2) // 10.12