用JavaScript获取数字的小数部分 [英] Get decimal portion of a number with JavaScript
本文介绍了用JavaScript获取数字的小数部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的浮动数字如 3.2
和 1.6
。
我需要把数字分成整数和小数部分。例如,
3.2
的值将被分成两个数字,即 3
和 0.2
获取整数部分很简单:
n = Math.floor(n);
但是我很难得到小数部分。
我试过这个:
remainer = n%2; // obtem a parte decimal do rating
但它并不总是正常工作。 b
$ b
前面的代码有以下输出:
n = 3.1 =>还有什么可以帮助你吗?
解决方案使用 1
,而不是 2
。 js> 2.3%1
0.2999999999999998
I have float numbers like 3.2
and 1.6
.
I need to separate the number into the integer and decimal part. For example, a value of 3.2
would be split into two numbers, i.e. 3
and 0.2
Getting the integer portion is easy:
n = Math.floor(n);
But I am having trouble getting the decimal portion.
I have tried this:
remainer = n % 2; //obtem a parte decimal do rating
But it does not always work correctly.
The previous code has the following output:
n = 3.1 => remainer = 1.1
What I am missing here?
解决方案 Use 1
, not 2
.
js> 2.3 % 1
0.2999999999999998
这篇关于用JavaScript获取数字的小数部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文