【问题标题】:How do I get the perfect format for my calculator?如何为我的计算器获得完美的格式?
【发布时间】:2020-02-14 02:04:01
【问题描述】:

我构建了一个计算器,我需要以完美格式输出的帮助。 我使用 C#/WindowsForms。 现在是格式 N:

    100 = 100,00
    1000 = 1.000,00
    more than 999 Trillion = 1.000.000.000.000.000,00 (no 10e+ x)
    0,123456789 = 0,16 (completes after two places)

我尝试创建自定义格式,但它没有按我想要的方式工作。 我想要的格式是:

    100 = 100
    1000 = 1.000
    1000000 = 1.000.000
    1000000000 = 1.000.000.000 
    higher 999 Trillion = Xe+ X
    0,111111111 = 0,111111111
    and at to much negative range = x.00000000000e -x

长话短说。 我需要 999.999.999.999.999 之后的 10e +/- 变体 正区域每 3 步需要一个点(1.000、100.000、1.000.000) 较小的数字(0,00000)只有一个逗号。 有人可以帮忙吗?

【问题讨论】:

    标签: c# winforms format calculator tostring


    【解决方案1】:

    不确定这是您最终想要的格式,但通过实施扩展,您可以设置自己的规则来实现您想要的结果。我使用的是西班牙文化信息,因为它使用逗号作为小数分隔符。

    public static class Extensions
    {
        public static string Formatted(this double d)
        {
            if (d >= 1e+15)
            {
                return d.ToString(CultureInfo.GetCultureInfo("es-ES"));
            }
            else
            {
                return d.ToString("#,##0.###############", CultureInfo.GetCultureInfo("es-ES"));
            }
        }
    }
    

    示例:

    var testValues = new List<double>()
    {
        100,
        1000,
        1000000,
        1000000000,
        1000000000000000,
        0.3,
        0.123456789,
    };
    
    foreach (var value in testValues)
    {
         Console.WriteLine(value.Formatted());
    }
    

    100

    1.000

    1.000.000

    1.000.000.000

    1E+15

    0,3

    0,123456789

    【讨论】:

    • 谢谢。它仍然不能完美地工作。只有积分或 10+ 工作。取决于我是否使用#。但我想我可以通过菜单将它整合为一种额外的格式。非常感谢:)
    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多