简体   繁体   English

String.Format具有无限精度和至少2个十进制数字

[英]String.Format with infinite precision and at least 2 decimal digit

I am developing an application for some guys very picky about numerical accuracy (they are dealing among others with accounting and very accurate telecom variables). 我正在为一些非常挑剔数字准确性的人开发一个应用程序(他们正在处理会计和非常准确的电信变量)。 For this reason I am using the decimal type everywhere, because the float and double types are not suitable (do not hesitate to redirect me to an existing better datatype). 出于这个原因,我到处都使用decimal类型,因为float和double类型不适合(不要犹豫,将我重定向到现有的更好的数据类型)。

My problem comes when I need to format those numbers. 当我需要格式化这些数字时,我的问题出现了。 The requirement is to display as many decimal digit as needed but at least 2 and also to use a group separator for thousands. 要求是根据需要显示尽可能多的十进制数字,但至少为2,并且还要使用组分隔符数千。

For example: 例如:

Value       Formatted
1                1.00
1000         1,000.00
1.5              1.50
1000.355    1,000.355
0.000035     0.000035

So I went the MSDN looking for numeric string formats. 所以我去MSDN寻找数字字符串格式。 I found this useful resource Standard Numeric Formats but none of my tries works as expected (N, N2, F, F2, G, G2, etc, I tried various combinations even when I didn't believe in them ^^ - I even try some F2# for fun). 我发现这个有用的资源标准数字格式但我的尝试没有按预期工作(N,N2,F,F2,G,G2等,即使我不相信它我尝试了各种组合^^ - 我甚至尝试一些F2#为了好玩)。

My conclusion is there is not a built-in format to do what I want. 我的结论是没有内置的格式来做我想做的事情。 Right? 对?

So I checked out the next chapter Custom Numeric Formats . 所以我查看了下一章Custom Numeric Formats But I couldn't find a combination that suit my needs. 但我找不到适合我需要的组合。 So I went to SO and find a lots of question about that ( 1 , 2 , and so on). 所以我去了SO,找到很多关于这个问题的( 12 ,依此类推)。

These questions let me fear that the only solution is this one: #,##0.00####### with as many trailing # as I need precision. 这些问题让我担心唯一的解决方案就是这个问题: #,##0.00#######有尽可能多的尾随#,因为我需要精度。

Am I right? 我对吗?

I guess that with 12 #, my guys won't find any accuracy issue, but I might have missed the magical format I need? 我想12#,我的家伙不会发现任何准确性问题,但我可能错过了我需要的神奇格式?

This is probably what you're looking for: 这可能是你正在寻找的:

static string FormatNumber(string input)
{
    var dec = decimal.Parse(input);
    var bits = decimal.GetBits(dec);
    var prec = bits[3] >> 16 & 255;
    if (prec < 2)
        prec = 2;
    return dec.ToString("N" + prec);
}

When you call it, do a ToString() on decimals, and convert the result back to decimal if needed. 当你调用它时,在小数上执行ToString() ,并在需要时将结果转换回十进制。

I tried your example numbers, and the result: 我尝试了你的示例数字,结果如下:

在此输入图像描述

Based on this SO answer . 基于这个SO答案

I created a little function: it formats the number based on how many decimal places it has: 我创建了一个小函数:它根据它具有的小数位数来格式化数字:

    public static void Main()
    {
        decimal theValue;
        theValue = 0.000035M;
        Console.WriteLine(theFormat(theValue));
        theValue = 1.5M;
        Console.WriteLine(theFormat(theValue));
        theValue = 1;
        Console.WriteLine(theFormat(theValue));                 
    }
    public static decimal theFormat(decimal theValue){
        int count = BitConverter.GetBytes(decimal.GetBits(theValue)[3])[2];
        return count > 1?theValue:Convert.ToDecimal(string.Format("{0:F2}", theValue));
    }

This, produces the following output: 这样,产生以下输出:

0.000035
1.50
1.00

If you want a total control over formatting you can implement your own IFormatProvider for decimals. 如果您想要完全控制格式化,可以为小数实现自己的IFormatProvider。 Inside of it you can use StringBuilder and do anything you need with no restrictions of string.Format(). 在其中你可以使用StringBuilder并做任何你需要的事情,没有string.Format()的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM