简体   繁体   English

计算数字位数的问题

[英]issue with counting digits of a number

Here is a code to count the digits of a given number.这是一个计算给定数字位数的代码。

There are two issues with this code that I can't fix without a hand:这段代码有两个问题,我无法不动手解决:

First : If we have the function like count(502.1000);第一:如果我们有像count(502.1000);这样的函数count(502.1000); the output for decimals would be 1 instead of 4 ...小数的输出将是 1 而不是 4 ...

Second : If we have a number without decimals like count(5024);第二:如果我们有一个没有小数的数字,比如count(5024); the output for numbers would be 1 instead of 4 ...数字的输出将是 1 而不是 4 ...

Here is the code:这是代码:

 count(502.134); // Desired result is Numbers: 3 Decimals 3 count(502.1000); // Desired result is Numbers: 3 Decimals 4 count(5024); // Desired result is Numbers: 4 Decimals 0 function count(num) { Number.prototype.countDecimals = function () { if(Math.floor(this.valueOf()) === this.valueOf()) return 0; return this.toString().split(".")[1].length || 0; } Number.prototype.countNumber = function () { if(Math.floor(this.valueOf()) === this.valueOf()) return 1; return this.toString().split(".")[0].length || 0; } var a = Math.abs(num).countNumber(); var b = Math.abs(num).countDecimals(); console.log('Numbers: ' + a + ' Decimals '+ + b) }

Sorry, I thought it would be quicker to write my own function than analyse your code:抱歉,我认为编写自己的函数比分析代码更快:

function getDigitLength(num) {
  if (!num) {
      return;
  }

  const numString = num.toString();
  const split = numString.split('.');

  const numbers = split[0].length;

  const decimals = split[1] ? split[1].length : 0;

  console.log('Numbers: ' + numbers + ' Decimals '+ decimals);
}

Made this really quick, some refactoring could be done.使这非常快,可以进行一些重构。 It should result in what you are after though.不过,它应该会导致您所追求的结果。

By the way, for this count(502.1000); // Desired result is Numbers: 3 Decimals 4顺便说一下,对于这个count(502.1000); // Desired result is Numbers: 3 Decimals 4 count(502.1000); // Desired result is Numbers: 3 Decimals 4 is difficult because javascript will convert 502.1000 to 502.1 . count(502.1000); // Desired result is Numbers: 3 Decimals 4很难,因为 javascript 会将502.1000转换为502.1 Only if you pass 502.1000 as a string will it work.只有当您将502.1000作为字符串传递502.1000 ,它才会起作用。

Try this .尝试这个 。 But only way(as pr my knowledge) 502.1000 will give proper result if you can convert that to string, not sure if that is possible in your implementation.但只有方法(据我所知)502.1000 才会给出正确的结果,如果您可以将其转换为字符串,不确定在您的实现中是否可行。

 var count = function(value) {
        var deccnt = 0;
        var numcnt = 0;
        try {
        deccnt = value.toString().split(".")[1].length || 0;
        } catch(e){}
        try {
        numcnt = value.toString().split(".")[0].length || 0;
        } catch(e){}
        alert(numcnt + " : " + deccnt);
    }

var num1 = 502.134;
    count(num1.toString())
    num1 = "502.1000";
    count(num1);
    num1 = 5024;
    count(num1.toString())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM