Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

c# - How to round System.Decimal in .Net to a number of significant figures

I have a System.Decimal number

0.00123456789

and I wish to round to 3 significant figures. I expect

0.00123

with the behaviour to be a rounding behaviour rather than truncation. Is there a bullet proof way to do this in .Net?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this... But I don't guarantee anything... Written and tested in 20 minutes and based on Pyrolistical's code from https://stackoverflow.com/a/1581007/613130 There is a big difference in that he uses a long for the shifted variable (because a double has a precision of 15-16 digits, while a long has 18-19, so a long is enough), while I use a decimal (because decimal has a precision of 28-29 digits).

public static decimal RoundToSignificantFigures(decimal num, int n)
{
    if (num == 0)
    {
        return 0;
    }

    // We are only looking for the next power of 10... 
    // The double conversion could impact in some corner cases,
    // but I'm not able to construct them...
    int d = (int)Math.Ceiling(Math.Log10((double)Math.Abs(num)));
    int power = n - d;

    // Same here, Math.Pow(10, *) is an integer number
    decimal magnitude = (decimal)Math.Pow(10, power);

    // I'm using the MidpointRounding.AwayFromZero . I'm not sure
    // having a MidpointRounding.ToEven would be useful (is Banker's
    // rounding used for significant figures?)
    decimal shifted = Math.Round(num * magnitude, 0, MidpointRounding.AwayFromZero);
    decimal ret = shifted / magnitude;

    return ret;
}

If you don't trust the (int)Math.Ceiling(Math.Log10((double) you could use this:

private static readonly decimal[] Pows = Enumerable.Range(-28, 57)
    .Select(p => (decimal)Math.Pow(10, p))
    .ToArray();

public static int Log10Ceiling(decimal num)
{
    int log10 = Array.BinarySearch(Pows, num);
    return (log10 >= 0 ? log10 : ~log10) - 28;
}

I have written it in another 20 minutes (and yes, I have tested all the Math.Pow((double), p) for all the values -28 - +28). It seems to work, and it's only 20% slower than the C# formula based on doubles). It's based on a static array of pows and a BinarySearch. Luckily the BinarySearch already "suggests" the next element when it can't find one :-), so the Ceiling is for free.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...