【问题标题】:String.Format an integer to use a thousands separator with decimal value in danish cultureString.Format 一个整数以在丹麦文化中使用带有十进制值的千位分隔符
【发布时间】:2013-12-05 09:48:29
【问题描述】:

我有一个字符串totalPRice,它的值类似于1147,5 我想要两件事。
1)将值四舍五入,使,后始终有两位数
2)在这个字符串中实现千位分隔符,这样最终的输出就会像这样1.147,50
我尝试过这样的事情

String.Format("{0:0.00}", totalPRice)

它通过生成输出1147,50 正确地满足了我的第一个要求。 但我的第二个要求远远落后。谁能告诉我如何实现这一目标?

注意:在丹麦文化中,. 代表,, 代表.

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    可以参考Standard Numeric Format Strings使用

    string.Format("{0:N2}", 1234.56)
    

    如果丹麦语不是您的默认文化,您也可以手动指定文化:

    var danishCulture = CultureInfo.CreateSpecificCulture("da-DK");
    string.Format(danishCulture, "{0:N2}", 1234.56);
    

    MSDN Reference for CultureInfo

    【讨论】:

      【解决方案2】:

      您应该创建一个特定于文化的CultureInfo 对象,并在将数字转换为字符串时使用它。另外,您可以设置default culture for your whole program

      然后,您的代码将如下所示:

      // create Dennmark-specific culture settings
      CultureInfo danishCulture = new CultureInfo("da");
      
      // format the number so that correct Danish decimal and group separators are used
      decimal totalPrice = 1234.5m;
      Console.WriteLine(totalPrice.ToString("#,###.##", danishCulture));
      

      请注意,格式化字符串中的., 可以根据需要指定为相反的。这是因为它们识别小数和组分隔符,并替换为正确的文化特定分隔符。

      【讨论】:

        【解决方案3】:

        试试这个:

        String.Format("{0:N2}", totalPRice)
        

        另一种可能性是使用ToString(string format) 重载。

        totalPRice.ToString("N2");
        

        【讨论】:

          【解决方案4】:

          如果这是一个货币值(钱!),那么最好使用当前格式说明符“C”或“c”:

          string.Format("{0:C}", 1234.56)
          

          通常我不写小数位数,因为它来自国际配置。 如果你不想使用默认的,你可以使用不同的 colture 说明符。

          var colture = CultureInfo.CreateSpecificCulture("§§§§§");
          string.Format(culture, "{0:C}", 1234.56);
          

          其中 §§§§§ 是标识所需颜色的字符串。

          【讨论】:

            【解决方案5】:

            试试这个价格格式。把它放在模板字段而不是BoundField下。

            <%#(decimal.Parse(Eval("YourDataField").ToString())).ToString("N2")%>
            

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多