Get number of digits before decimal point

前端 未结 25 1837
独厮守ぢ
独厮守ぢ 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 12:20

    If you have a bias towards smaller numbers, you can use something more simple like this.

    It is split up into two methods, so the first method is smaller and can be inlined.

    Performance is about the same as the solution with the Log10, but without the rounding errors. The Method using Log10, is still the fastest (a bit) specially for numbers > 1 million.

        public static int CountNrOfDigitsIfs(decimal d) {
    
            var absD = Math.Abs(d);
            // 1
            if (absD < 10M) return 1;
            // 2
            if (absD < 100M) return 2;
            // 3
            if (absD < 1000M) return 3;
            // 4
            if (absD < 10000M) return 4;
    
            return CountNrOfDigitsIfsLarge(d);
        }
    
        private static int CountNrOfDigitsIfsLarge(decimal d) {
    
            // 5
            if (d < 100000M) return 5;
            // 6
            if (d < 1000000M) return 6;
            // 7
            if (d < 10000000M) return 7;
            // 8
            if (d < 100000000M) return 8;
            // 9
            if (d < 1000000000M) return 9;
            // 10
            if (d < 10000000000M) return 10;
            // 11
            if (d < 100000000000M) return 11;
            // 12
            if (d < 1000000000000M) return 12;
            // 13
            if (d < 10000000000000M) return 13;
            // 14
            if (d < 100000000000000M) return 14;
            // 15
            if (d < 1000000000000000M) return 15;
            // 16
            if (d < 10000000000000000M) return 16;
            // 17
            if (d < 100000000000000000M) return 17;
            // 18
            if (d < 1000000000000000000M) return 18;
            // 19
            if (d < 10000000000000000000M) return 19;
            // 20
            if (d < 100000000000000000000M) return 20;
            // 21
            if (d < 1000000000000000000000M) return 21;
            // 22
            if (d < 10000000000000000000000M) return 22;
            // 23
            if (d < 100000000000000000000000M) return 23;
            // 24
            if (d < 1000000000000000000000000M) return 24;
            // 25
            if (d < 10000000000000000000000000M) return 25;
            // 26
            if (d < 100000000000000000000000000M) return 26;
            // 27
            if (d < 1000000000000000000000000000M) return 27;
            // 28
            if (d < 10000000000000000000000000000M) return 28;
    
            return 29; // Max nr of digits in decimal
        }
    

    This code is generated using the following T4 template:

    <#
       const int SIGNIFICANT_DECIMALS = 29;
       const int SPLIT = 5;
    #>
    
    namespace Study.NrOfDigits {
        static partial class DigitCounter {
    
            public static int CountNrOfDigitsIfs(decimal d) {
    
                var absD = Math.Abs(d);
    <#          
                for (int i = 1; i < SPLIT; i++) { // Only 29 significant digits
                   var zeroes = new String('0', i);
    #>
                // <#= i #>
                if (absD < 1<#= zeroes #>M) return <#= i #>;
    <# 
                }
    #>
    
                return CountNrOfDigitsIfsLarge(d);
            }
    
            private static int CountNrOfDigitsIfsLarge(decimal d) {
    
    <#          
                for (int i = SPLIT; i < SIGNIFICANT_DECIMALS; i++) { // Only 29 significant digits
                   var zeroes = new String('0', i);
    #>
                // <#= i #>
                if (d < 1<#= zeroes #>M) return <#= i #>;
    <# 
                }
    #>
    
                return <#= SIGNIFICANT_DECIMALS #>; // Max nr of digits in decimal
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题