小数点后的千位分隔符

时间:2018-10-02 10:49:25

标签: c# number-formatting decimal-point

如何获取数字格式,以便在小数点后也使用(特定于文化的)千位分隔符?

检查此代码(NUnit测试用例,按原样通过):

    [Test]
    [SetCulture("en-US")]
    public void FormattingOnly()
    {
        // a large number
        double result = 1E9 + 0.212324;
        string printout = result.ToString("N9");
        // Would like to have this as "1,000,000,000.212,320,000"
        Assert.AreEqual("1,000,000,000.212320000", printout); 

        result = 0.212324;
        printout = result.ToString("N9");
        // Hard to read, should better be "0.212,324,000"
        Assert.AreEqual("0.212324000", printout);

        result = 0.212324;
        printout = result.ToString("E9");
        // Even harder to read, needs users to have a PhD ;)
        Assert.AreEqual("2.123240000E-001", printout);

        result = 1E9 + 0.212324;
        printout = result.ToString("E");
        // Completely worthless for many users
        Assert.AreEqual("1.000000E+009", printout);
    }

我正在尝试找到最易读的格式,即使没有数学背景的用户也可以理解。在我的应用程序中,用户可以根据自己的需要设置输出格式中的位数。

注意:这不是.NET String.Format() to add commas in thousands place for a number的重复项。一般而言,大约有数千个分隔符。

0 个答案:

没有答案