Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting numbers with significant figures in C#

I have some decimal data that I am pushing into a SharePoint list where it is to be viewed. I'd like to restrict the number of significant figures displayed in the result data based on my knowledge of the specific calculation. Sometimes it'll be 3, so 12345 will become 12300 and 0.012345 will become 0.0123. Occasionally it will be 4 or 5. Is there any convenient way to handle this?

like image 465
Chris Farmer Avatar asked Oct 01 '08 15:10

Chris Farmer


People also ask

How do you change to 3 significant figures?

We round a number to three significant figures in the same way that we would round to three decimal places. We count from the first non-zero digit for three digits. We then round the last digit. We fill in any remaining places to the right of the decimal point with zeros.

How do you set significant figures in C++?

To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).

How do you round off in coding?

Examples: Input : n = 139.59 d = 4 Output : The number after rounding-off is 139.6 . The number 139.59 has 5 significant figures and for rounding-off the number to 4 significant figures, 139.59 is converted to 139.6 . Input : n = 1240 d = 2 Output : The number after rounding-off is 1200 .

How many significant figures does 0.0100 have?

Trailing zeros after a decimal point count if preceded by a non-zero value. Example: 0.01 one significant figure, 0.010 two significant figures, 0.0100 three significant figures.


1 Answers

See: RoundToSignificantFigures by "P Daddy".
I've combined his method with another one I liked.

Rounding to significant figures is a lot easier in TSQL where the rounding method is based on rounding position, not number of decimal places - which is the case with .Net math.round. You could round a number in TSQL to negative places, which would round at whole numbers - so the scaling isn't needed.

Also see this other thread. Pyrolistical's method is good.

The trailing zeros part of the problem seems like more of a string operation to me, so I included a ToString() extension method which will pad zeros if necessary.

using System; using System.Globalization;  public static class Precision {     // 2^-24     public const float FLOAT_EPSILON = 0.0000000596046448f;      // 2^-53     public const double DOUBLE_EPSILON = 0.00000000000000011102230246251565d;      public static bool AlmostEquals(this double a, double b, double epsilon = DOUBLE_EPSILON)     {         // ReSharper disable CompareOfFloatsByEqualityOperator         if (a == b)         {             return true;         }         // ReSharper restore CompareOfFloatsByEqualityOperator          return (System.Math.Abs(a - b) < epsilon);     }      public static bool AlmostEquals(this float a, float b, float epsilon = FLOAT_EPSILON)     {         // ReSharper disable CompareOfFloatsByEqualityOperator         if (a == b)         {             return true;         }         // ReSharper restore CompareOfFloatsByEqualityOperator          return (System.Math.Abs(a - b) < epsilon);     } }  public static class SignificantDigits {     public static double Round(this double value, int significantDigits)     {         int unneededRoundingPosition;         return RoundSignificantDigits(value, significantDigits, out unneededRoundingPosition);     }      public static string ToString(this double value, int significantDigits)     {         // this method will round and then append zeros if needed.         // i.e. if you round .002 to two significant figures, the resulting number should be .0020.          var currentInfo = CultureInfo.CurrentCulture.NumberFormat;          if (double.IsNaN(value))         {             return currentInfo.NaNSymbol;         }          if (double.IsPositiveInfinity(value))         {             return currentInfo.PositiveInfinitySymbol;         }          if (double.IsNegativeInfinity(value))         {             return currentInfo.NegativeInfinitySymbol;         }          int roundingPosition;         var roundedValue = RoundSignificantDigits(value, significantDigits, out roundingPosition);          // when rounding causes a cascading round affecting digits of greater significance,          // need to re-round to get a correct rounding position afterwards         // this fixes a bug where rounding 9.96 to 2 figures yeilds 10.0 instead of 10         RoundSignificantDigits(roundedValue, significantDigits, out roundingPosition);          if (Math.Abs(roundingPosition) > 9)         {             // use exponential notation format             // ReSharper disable FormatStringProblem             return string.Format(currentInfo, "{0:E" + (significantDigits - 1) + "}", roundedValue);             // ReSharper restore FormatStringProblem         }          // string.format is only needed with decimal numbers (whole numbers won't need to be padded with zeros to the right.)         // ReSharper disable FormatStringProblem         return roundingPosition > 0 ? string.Format(currentInfo, "{0:F" + roundingPosition + "}", roundedValue) : roundedValue.ToString(currentInfo);         // ReSharper restore FormatStringProblem     }      private static double RoundSignificantDigits(double value, int significantDigits, out int roundingPosition)     {         // this method will return a rounded double value at a number of signifigant figures.         // the sigFigures parameter must be between 0 and 15, exclusive.          roundingPosition = 0;          if (value.AlmostEquals(0d))         {             roundingPosition = significantDigits - 1;             return 0d;         }          if (double.IsNaN(value))         {             return double.NaN;         }          if (double.IsPositiveInfinity(value))         {             return double.PositiveInfinity;         }          if (double.IsNegativeInfinity(value))         {             return double.NegativeInfinity;         }          if (significantDigits < 1 || significantDigits > 15)         {             throw new ArgumentOutOfRangeException("significantDigits", value, "The significantDigits argument must be between 1 and 15.");         }          // The resulting rounding position will be negative for rounding at whole numbers, and positive for decimal places.         roundingPosition = significantDigits - 1 - (int)(Math.Floor(Math.Log10(Math.Abs(value))));          // try to use a rounding position directly, if no scale is needed.         // this is because the scale mutliplication after the rounding can introduce error, although          // this only happens when you're dealing with really tiny numbers, i.e 9.9e-14.         if (roundingPosition > 0 && roundingPosition < 16)         {             return Math.Round(value, roundingPosition, MidpointRounding.AwayFromZero);         }          // Shouldn't get here unless we need to scale it.         // Set the scaling value, for rounding whole numbers or decimals past 15 places         var scale = Math.Pow(10, Math.Ceiling(Math.Log10(Math.Abs(value))));          return Math.Round(value / scale, significantDigits, MidpointRounding.AwayFromZero) * scale;     } } 
like image 75
HAL9000 Avatar answered Sep 19 '22 19:09

HAL9000