如何从 JavaScript 数字中删除小数部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7641818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove the decimal part from JavaScript number?
提问by JacobTheDev
I have the results of a division and I wish to discard the decimal portion of the resultant number.
我有一个除法的结果,我希望丢弃结果数字的小数部分。
How can I do this?
我怎样才能做到这一点?
回答by alex
You could use...
你可以用...
Math.trunc()
(truncate fractional part, also see below)Math.floor()
(round down)Math.ceil()
(round up)Math.round()
(round to nearest integer)
Math.trunc()
(截断小数部分,另见下文)Math.floor()
(向下舍入)Math.ceil()
(围捕)Math.round()
(四舍五入到最接近的整数)
...dependent on how you wanted to remove the decimal.
...取决于您想如何删除小数。
Math.trunc()
isn't supported on all platforms yet (namely IE), but you could easily use a polyfillin the meantime.
Math.trunc()
尚不支持所有平台(即 IE),但在此期间您可以轻松使用polyfill。
Another method of truncating the fractional portion with excellent platform support is by using a bitwise operator(.e.g |0
). The side-effect of using a bitwise operator on a number is it will treat its operand as a signed 32bit integer, therefore removing the fractional component. Keep in mind this will also mangle numbers larger than 32 bits.
另一种截断具有出色平台支持的小数部分的方法是使用按位运算符(.eg |0
)。对数字使用按位运算符的副作用是它将其操作数视为有符号的 32 位整数,因此删除了小数部分。请记住,这也会破坏大于 32 位的数字。
You may also be talking about the inaccuracy of decimal rounding with floating point arithmetic.
您可能还在谈论使用浮点运算进行小数舍入的不准确性。
Required Reading - What Every Computer Scientist Should Know About Floating-Point Arithmetic.
回答by Braden Steffaniak
You can also use bitwise operators to truncate the decimal.
您还可以使用按位运算符截断小数。
e.g.
例如
var x = 9 / 2;
console.log(x); // 4.5
x = ~~x;
console.log(x); // 4
x = -3.7
console.log(~~x) // -3
console.log(x | 0) // -3
console.log(x << 0) // -3
Bitwise operations are considerably more efficient than the Math functions. The double not bitwise operator also seems to slightly outperform the x | 0
and x << 0
bitwise operations by a negligible amount.
按位运算比数学函数高效得多。double not bitwise 运算符似乎也略胜于x | 0
和x << 0
按位运算,但可以忽略不计。
// 952 milliseconds
for (var i = 0; i < 1000000; i++) {
(i * 0.5) | 0;
}
// 1150 milliseconds
for (var i = 0; i < 1000000; i++) {
(i * 0.5) << 0;
}
// 1284 milliseconds
for (var i = 0; i < 1000000; i++) {
Math.trunc(i * 0.5);
}
// 939 milliseconds
for (var i = 0; i < 1000000; i++) {
~~(i * 0.5);
}
Also worth noting is that the bitwise not operator takes precedence over arithmetic operations, so you may need to surround calculations with parentheses to have the intended result:
另外值得注意的是,按位非运算符优先于算术运算,因此您可能需要用括号将计算括起来以获得预期结果:
x = -3.7
console.log(~~x * 2) // -6
console.log(x * 2 | 0) // -7
console.log(x * 2 << 0) // -7
console.log(~~(x * 2)) // -7
console.log(x * 2 | 0) // -7
console.log(x * 2 << 0) // -7
More info about the double bitwise not operator can be found at Double bitwise NOT (~~)
有关 double bitwise not 运算符的更多信息可以在Double bitwise NOT (~~)
回答by Hari Pachuveetil
You could also do
你也可以这样做
parseInt(a/b)
回答by Mahdi ghafoorian
u can also show a certain number of digit after decimal point(here 2 digits) using following code :
您还可以使用以下代码在小数点后显示一定数量的数字(此处为 2 位数字):
var num = (15.46974).toFixed(2)
console.log(num) // 15.47
console.log(typeof num) // string
回答by Dave Newton
Use Math.round()
.
使用Math.round()
.
(Alex's answer is better; I made an assumption :)
(亚历克斯的回答更好;我做了一个假设:)
回答by Navdeep Singh
Use Math.round()
function.
使用Math.round()
功能。
Math.round(65.98) // will return 66
Math.round(65.28) // will return 65
回答by Chad von Nau
With ES2015, Math.trunc()is available.
在 ES2015 中,Math.trunc()可用。
Math.trunc(2.3) // 2
Math.trunc(-2.3) // -2
Math.trunc(22222222222222222222222.3) // 2.2222222222222223e+22
Math.trunc("2.3") // 2
Math.trunc("two") // NaN
Math.trunc(NaN) // NaN
It's not supported in IE11 or below, but does work in Edge and every other modern browser.
它在 IE11 或更低版本中不受支持,但在 Edge 和其他所有现代浏览器中都可以使用。
回答by Helzgate
If you don't care about rouding, just convert the number to a string, then remove everything after the period including the period. This works whether there is a decimal or not.
如果您不关心rouding,只需将数字转换为字符串,然后删除句点之后的所有内容,包括句点。无论是否有小数,这都有效。
const sEpoch = ((+new Date()) / 1000).toString();
const formattedEpoch = sEpoch.split('.')[0];
回答by sreepurna
Here is the compressive in detailed explanation with the help of above posts:
这是在上述帖子的帮助下压缩的详细解释:
1. Math.trunc() :It is used to remove those digits which are followed by dot. It converts implicitly. But, not supported in IE.
1. Math.trunc() :用于去除后面有点的数字。它隐式转换。但是,在 IE 中不支持。
Example:
例子:
Math.trunc(10.5) // 10
Math.trunc(10.5) // 10
Math.trunc(-10.5) // -10
Math.trunc(-10.5) // -10
Other Alternative way:Use of bitwise not operator:
其他替代方法:使用按位非运算符:
Example:
例子:
x= 5.5
x= 5.5
~~x // 5
~~x // 5
2. Math.floor() :It is used to give the minimum integer value posiible. It is supported in all browsers.
2. Math.floor() :用于给出可能的最小整数值。所有浏览器都支持它。
Example:
例子:
Math.floor(10.5) // 10
Math.floor(10.5) // 10
Math.floor( -10.5) // -11
Math.floor( -10.5) // -11
3. Math.ceil() :It is used to give the highest integer value possible. It is supported in all browsers.
3. Math.ceil() :用于给出可能的最高整数值。所有浏览器都支持它。
Example:
例子:
Math.ceil(10.5) // 11
Math.ceil(10.5) // 11
Math.ceil(-10.5) // -10
Math.ceil(-10.5) // -10
4. Math.round() :It is rounded to the nearest integer. It is supported in all browsers.
4. Math.round() :四舍五入到最接近的整数。所有浏览器都支持它。
Example:
例子:
Math.round(10.5) // 11
Math.round(10.5) // 11
Math.round(-10.5)// -10
Math.round(-10.5)// -10
Math.round(10.49) // 10
Math.round(10.49) // 10
Math.round(-10.51) // -11
Math.round(-10.51) // -11
回答by Shahar H
toFixed will behave like round.
toFixed 将表现得像圆形。
For a floor like behavior use %:
对于类似地板的行为,请使用 %:
var num = 3.834234;
var floored_num = num - (num % 1); // floored_num will be 3