Get number of digits before decimal point

前端 未结 25 1829
独厮守ぢ
独厮守ぢ 2020-12-28 11:53

I have a variable of decimal type and I want to check the number of digits before decimal point in it. What should I do? For example, 467.45 should

相关标签:
25条回答
  • 2020-12-28 11:53
    decimal d = 467.45M;
    int i = (int)d;
    Console.WriteLine(i.ToString(CultureInfo.InvariantCulture).Length); //3
    

    As a method;

    public static int GetDigitsLength(decimal d)
    {
      int i = int(d);
      return i.ToString(CultureInfo.InvariantCulture).Length;
    }
    

    Note: Of course you should check first your decimals full part is bigger than Int32.MaxValue or not. Because if it is, you get an OverflowException.

    Is such a case, using long instead of int can a better approach. However even a long (System.Int64) is not big enough to hold every possible decimal value.

    As Rawling mentioned, your full part can hold the thousands separator and my code will be broken in such a case. Because in this way, it totally ignores my number contains NumberFormatInfo.NumberGroupSeparator or not.

    That's why getting numbers only is a better approach. Like;

    i.ToString().Where(c => Char.IsDigit(c)).ToArray()
    
    0 讨论(0)
  • 2020-12-28 11:53

    Math.Floor(Math.Log10((double)n) + 1); is the way to go.

    Converting to int is BAD because decimal may be bigger than int:

    Decimal.MaxValue = 79,228,162,514,264,337,593,543,950,335;
    Int32.MaxValue = 2,147,483,647; //that is, hexadecimal 0x7FFFFFFF;
    

    Math.Floor(n).ToString().Count(); is bad because it may include thousands seperators.

    0 讨论(0)
  • 2020-12-28 11:53

    TLDR all the other answers. I wrote this in PHP, and the math would be the same. (If I knew C# I'd have written in that language.)

    $input=21689584.999;
    
        $input=abs($input);
    $exp=0;
    do{
      $test=pow(10,$exp);
    
      if($test > $input){
        $digits=$exp;
      }
      if($test == $input){
        $digits=$exp+1;
      }
      $exp++;
    }while(!$digits);
    if($input < 1){$digits=0;}
    echo $digits;
    

    I don't doubt there's a better way, but I wanted to throw in my $.02

    EDIT:

    I php-ized the code I mentioned in my comments, but did away with the int conversion.

    function digitCount($input){
      $digits=0;
      $input=abs($input);
        while ($input >= 1) {
          $digits++;
          $input=$input/10;
          //echo $input."<br>";
        }
      return $digits;   
    }
    $big=(float)(PHP_INT_MAX * 1.1);
    echo digitCount($big);
    
    0 讨论(0)
  • 2020-12-28 11:57

    The other solutions will lose digits if the number is too large.

    public int Digits(Decimal i)
    {
        NumberFormatInfo format = CultureInfo.CurrentCulture.NumberFormat;
        var str = Math.Abs(i).ToString().Replace(format.NumberGroupSeparator, "");
        var index = str.IndexOf(format.NumberDecimalSeparator);
        var digits = index == -1 ? str.Length : index;
    }
    
    0 讨论(0)
  • 2020-12-28 11:59

    I would try this:

    Math.Truncate(467.45).ToString().Length
    

    If you want to be sure not having some weird results for different cultures and with negative decimals, you better use this:

    var myDecimal = 467.45m;
    Math.Truncate(Math.Abs(myDecimal)).ToString(CultureInfo.InvariantCulture).Length
    
    0 讨论(0)
  • 2020-12-28 12:03

    I haven't tested this but I would keep it straightforward and do:

    decimal value = 467.45;
    string str = Convert.toString(value); // convert your decimal type to a string
    string[] splitStr = str.split('.'); // split it into an array (use comma separator assuming you know your cultural context)
    Console.WriteLine(splitStr[0].Count); // get the first element. You can also get the number of figures after the point by indexing the next value in the array.
    

    This does not handle negative numbers. If you care about those then considering taking the absolute value. Furthermore, if you want 0 before the decimal place to not be counted then you can use a simple if statement to check it.

    0 讨论(0)
提交回复
热议问题