Round a decimal to the first decimal position that is not zero
I want to shorten a number to the first significant digit that is not 0. The digits behind should be rounded. Examples: 0.001 -> 0.001 0.00367 -> 0.004 0.00337 -> 0.003 0.000000564 -> 0.0000006 0.00000432907543029 -> 0.000004 Currently I have the following procedure: if (value < (decimal) 0.01) { value = Math.Round(value, 4); } Note: numbers will always be positive the number of significant digits will always be 1 values larger 0.01 will always be rounded to two decimal places, hence the if < 0.01 As you can see from the examples above, a rounding to 4 Decimal places might not be enough and the value might vary greatly. I would declare precision variable and use a loop iteration multiplies that variable by 10 with the original value it didn't hit, that precision will add 1. then use precision variable be Math.Round second parameter. static decimal RoundFirstSignificantDigit(decimal input) { int precision = 0; var val = input; while (Math.A...