jQuery 在 JavaScript 中获取数字中小数位数的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17369098/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:54:56  来源:igfitidea点击:

Simplest way of getting the number of decimals in a number in JavaScript

javascriptjquerynumbersmootoolsdecimal-point

提问by PhilTrep

Is there a better way of figuring out the number of decimals on a number than in my example?

有没有比我的例子更好的方法来计算一个数字的小数位数?

var nbr = 37.435.45;
var decimals = (nbr!=Math.floor(nbr))?(nbr.toString()).split('.')[1].length:0;

By better I mean faster to execute and/or using a native JavaScript function, ie. something like nbr.getDecimals().

更好是指更快地执行和/或使用本机 JavaScript 函数,即。类似于 nbr.getDecimals()。

Thanks in advance!

提前致谢!

EDIT:

编辑:

After modifying series0ne answer, the fastest way I could manage is:

修改 series0ne 答案后,我可以管理的最快方法是:

var val = 37.435345;
var countDecimals = function(value) {
    if (Math.floor(value) !== value)
        return value.toString().split(".")[1].length || 0;
    return 0;
}
countDecimals(val);

Speed test: http://jsperf.com/checkdecimals

速度测试:http: //jsperf.com/checkdecimals

回答by Matthew Layton

Number.prototype.countDecimals = function () {
    if(Math.floor(this.valueOf()) === this.valueOf()) return 0;
    return this.toString().split(".")[1].length || 0; 
}

When bound to the prototype, this allows you to get the decimal count (countDecimals();) directly from a number variable.

当绑定到原型时,这允许您countDecimals();直接从数字变量中获取十进制计数 ( )。

E.G.

例如

var x = 23.453453453;
x.countDecimals(); // 9

It works by converting the number to a string, splitting at the .and returning the last part of the array, or 0 if the last part of the array is undefined (which will occur if there was no decimal point).

它的工作原理是将数字转换为字符串,在. 并返回数组的最后一部分,如果数组的最后一部分未定义,则返回 0(如果没有小数点,则会发生这种情况)。

If you do not want to bind this to the prototype, you can just use this:

如果你不想把它绑定到原型,你可以使用这个:

var countDecimals = function (value) {
    if(Math.floor(value) === value) return 0;
    return value.toString().split(".")[1].length || 0; 
}

回答by Pete D

Adding to series0ne answer if you want to have the code not throw an error for an integer number and get a result of 0 when there are no decimals use this:

添加到 series0ne 答案,如果您希望代码不为整数抛出错误并在没有小数时得到 0 结果,请使用以下命令:

var countDecimals = function (value) { 
    if ((value % 1) != 0) 
        return value.toString().split(".")[1].length;  
    return 0;
};

回答by Ricola3D

Regex are very very not efficient in calculations, they are mean if you can go another way. So I would avoid them :)

正则表达式的计算效率非常低,如果您可以采用另一种方式,则它们很有意义。所以我会避开它们:)

Things like "Number % 1" return the rounded decimal value (2.3 % 1 = 0.2999999999999998), and in a general way I would advice you to use strings as early as possible, since approximations with real numbers can change the number of decimals.

诸如“Number % 1”之类的东西返回四舍五入的十进制值(2.3 % 1 = 0.2999999999999998),一般来说,我建议您尽早使用字符串,因为实数的近似值会改变小数位数。

So yours is fine, but I'll look a way to optimize it.

所以你的很好,但我会寻找优化它的方法。

Edit:

编辑:

function CountDecimalDigits(number)
{
  var char_array = number.toString().split(""); // split every single char
  var not_decimal = char_array.lastIndexOf(".");
  return (not_decimal<0)?0:char_array.length - not_decimal;
}