Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 524473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:34:17+00:00 2026-05-13T08:34:17+00:00

I have some decimal data that I am pushing into a SharePoint list where

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:34:18+00:00Added an answer on May 13, 2026 at 8:34 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The parser needs to know the FPI and how to… May 13, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer Effective Java: Favor composition over inheritance. (This actually comes from… May 13, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer First, were you using a DOCTYPE flag when you test… May 13, 2026 at 3:30 pm

Related Questions

I'm performing some data type conversions where I need to represent uint , long
I am sure that I have made some painfully obvious blunder(s) that I just
I have a small database that I need help designing. I have a VB.NET
I have a problem, i didn't found yet the solution so i am asking
I am trying to figure out exactly what these new fangled data stores such

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.