Finding the number of places after the decimal point of a Double

后端 未结 8 1165
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 19:39

I have a Double value:

double a = 4.5565;

What is the easiest way to calculate the number of digits after the decimal point (4 in this case

8条回答
  •  盖世英雄少女心
    2020-12-15 20:39

    I think this might be a solution:

     private static int getDecimalCount(double val)
     {
         int i=0;
         while (Math.Round(val, i) != val)
             i++;
         return i;
     }
    
    double val9 = 4.5565d; int count9 = getDecimalCount(val9);//result: 4
    

    Sorry for the duplication -> https://stackoverflow.com/a/35238462/1266873

提交回复
热议问题