• <bdo id='bF3xR'></bdo><ul id='bF3xR'></ul>

    1. <i id='bF3xR'><tr id='bF3xR'><dt id='bF3xR'><q id='bF3xR'><span id='bF3xR'><b id='bF3xR'><form id='bF3xR'><ins id='bF3xR'></ins><ul id='bF3xR'></ul><sub id='bF3xR'></sub></form><legend id='bF3xR'></legend><bdo id='bF3xR'><pre id='bF3xR'><center id='bF3xR'></center></pre></bdo></b><th id='bF3xR'></th></span></q></dt></tr></i><div id='bF3xR'><tfoot id='bF3xR'></tfoot><dl id='bF3xR'><fieldset id='bF3xR'></fieldset></dl></div>
      <legend id='bF3xR'><style id='bF3xR'><dir id='bF3xR'><q id='bF3xR'></q></dir></style></legend>

      1. <tfoot id='bF3xR'></tfoot>

        <small id='bF3xR'></small><noframes id='bF3xR'>

        十进制字符串与千位分隔符?

        时间:2023-08-25
        <i id='BdUqu'><tr id='BdUqu'><dt id='BdUqu'><q id='BdUqu'><span id='BdUqu'><b id='BdUqu'><form id='BdUqu'><ins id='BdUqu'></ins><ul id='BdUqu'></ul><sub id='BdUqu'></sub></form><legend id='BdUqu'></legend><bdo id='BdUqu'><pre id='BdUqu'><center id='BdUqu'></center></pre></bdo></b><th id='BdUqu'></th></span></q></dt></tr></i><div id='BdUqu'><tfoot id='BdUqu'></tfoot><dl id='BdUqu'><fieldset id='BdUqu'></fieldset></dl></div>
          <tbody id='BdUqu'></tbody>
            1. <small id='BdUqu'></small><noframes id='BdUqu'>

              • <tfoot id='BdUqu'></tfoot>
              • <legend id='BdUqu'><style id='BdUqu'><dir id='BdUqu'><q id='BdUqu'></q></dir></style></legend>
                  <bdo id='BdUqu'></bdo><ul id='BdUqu'></ul>

                  本文介绍了十进制字符串与千位分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  考虑一个十进制值:

                  Decimal value = -1234567890.1234789012M;
                  

                  我想将此十进制 值转换为字符串,并包含千位分隔符".

                  i want to convert this Decimal value to a string, and include "thousands separators".

                  注意:我不想包含千位分隔符,我想包含数字分组.对于不将数字分组为数千或不使用逗号分隔组的文化,这种差异很重要

                  Note: i don't want to include thousand's separators, i want to include digit grouping. The difference is important for cultures that don't group numbers into thousands, or don't use commas to separate groups

                  在我的计算机上使用我当前的语言环境使用不同标准格式字符串的一些示例输出:

                  Some example output with different standard formatting strings, on my computer, with my current locale:

                  value.ToString()        =  -1234567890..1234789012   (Implicit General)
                  value.ToString("g")     =  -1234567890..1234789012   (General)
                  value.ToString("d")     =          FormatException   (Decimal whole number)
                  value.ToString("e")     =         -1..234568e++009   (Scientific)
                  value.ToString("f")     =         -1234567890..123   (Fixed Point)
                  value.ToString("n")     =     -12,,3456,,7890..123   (Number with commas for thousands)
                  value.ToString("r")     =          FormatException   (Round trippable)
                  value.ToString("c")     =   -$$12,,3456,,7890..123   (Currency)
                  value.ToString("#,0.#") =     -12,,3456,,7890..1
                  

                  想要(取决于文化)是:

                  en-US      -1,234,567,890.1234789012
                  ca-ES      -1.234.567.890,1234789012
                  gsw-FR     -1 234 567 890,1234789012    (12/1/2012: fixed gws-FR to gsw-FR)
                  fr-CH      -1'234'567'890.1234789012
                  ar-DZ       1,234,567,890.1234789012-
                  prs-AF      1.234.567.890,1234789012-
                  ps-AF       1،234،567،890,1234789012-
                  as-IN     -1,23,45,67,890.1234789012
                  lo-LA      (1234567,890.1234789012)     (some debate if numbers should be "1,234,567,890")
                  qps-PLOC  12,,3456,,7890..1234789012
                  

                  如何将 Decimal 转换为带有数字分组的字符串?

                  How can i convert a Decimal to a string, with digit groupings?

                  更新:更多期望输出,使用我目前的文化:

                  Update: Some more desired output, using my current culture of :

                  -1234567890M             -->   -12,,3456,,7890
                  -1234567890.1M           -->   -12,,3456,,7890..1
                  -1234567890.12M          -->   -12,,3456,,7890..12
                  -1234567890.123M         -->   -12,,3456,,7890..123
                  -1234567890.1234M        -->   -12,,3456,,7890..1234
                  -1234567890.12347M       -->   -12,,3456,,7890..12347
                  -1234567890.123478M      -->   -12,,3456,,7890..123478
                  -1234567890.1234789M     -->   -12,,3456,,7890..1234789
                  -1234567890.12347890M    -->   -12,,3456,,7890..1234789
                  -1234567890.123478901M   -->   -12,,3456,,7890..123478901
                  -1234567890.1234789012M  -->   -12,,3456,,7890..1234789012
                  

                  <小时>

                  更新:我尝试查看 Decimal.ToString() 如何设法使用 General 格式来显示它需要的所有数字显示:


                  Update: i tried peeking at how Decimal.ToString() manages to use the General format to show all the digits that it needs to show:

                  public override string ToString()
                  {
                      return Number.FormatDecimal(this, null, NumberFormatInfo.CurrentInfo);
                  }
                  

                  除了 Number.FormatDecimal 隐藏在某处:

                  [MethodImpl(MethodImplOptions.InternalCall)]
                  public static extern string FormatDecimal(decimal value, string format, NumberFormatInfo info);
                  

                  所以这是一条死胡同.

                  推荐答案

                  您可以指定自定义模式(该模式将适当地解析为特定于文化的分组方法以及适当的分组和小数分隔符).一个模式可以有正、负和零部分.积极模式始终相同,但消极模式取决于文化,可以从 NumberFormatInfo 的 NumberNegativePattern 属性.由于您希望尽可能地精确,因此需要在小数点后填写 28 位占位符;逗号强制分组.

                  You can specify a custom pattern (the pattern will appropriately resolve to the culture specific method of grouping and the appropriate grouping and decimal separator characters). A pattern can have positive, negative and zero sections. The positive pattern is always the same but the negative pattern depends on the culture and can be retrieved from the NumberFormatInfo's NumberNegativePattern property. Since you want as much precision as possible, you need to fill out 28 digit placeholders after the decimal; the comma forces grouping.

                      public static class DecimalFormatters
                      {
                          public static string ToStringNoTruncation(this Decimal n, IFormatProvider format)
                          {
                              NumberFormatInfo nfi = NumberFormatInfo.GetInstance(format);
                              string[] numberNegativePatterns = {
                                      "(#,0.############################)", //0:  (n)
                                      "-#,0.############################",  //1:  -n
                                      "- #,0.############################", //2:  - n
                                      "#,0.############################-",  //3:  n-
                                      "#,0.############################ -"};//4:  n -
                              var pattern = "#,0.############################;" + numberNegativePatterns[nfi.NumberNegativePattern];
                              return n.ToString(pattern, format);
                          }
                  
                          public static string ToStringNoTruncation(this Decimal n)
                          {
                              return n.ToStringNoTruncation(CultureInfo.CurrentCulture);
                          }
                      }
                  

                  样本输出

                  Locale    Output
                  ========  ============================
                  en-US     -1,234,567,890.1234789012
                  ca-ES     -1.234.567.890,1234789012
                  hr-HR     - 1.234.567.890,1234789012
                  gsw-FR    -1 234 567 890,1234789012
                  fr-CH     -1'234'567'890.1234789012
                  ar-DZ     1,234,567,890.1234789012-
                  prs-AF    1.234.567.890,1234789012-
                  ps-AF     1،234،567،890,1234789012-
                  as-IN     -1,23,45,67,890.1234789012
                  lo-LA     (1234567,890.1234789012)
                  qps-PLOC  -12,,3456,,7890..1234789012
                  

                  目前没有使用 NegativeNumberFormat 4 (n -) 的语言环境,因此无法测试这种情况.但没有理由认为它会失败.

                  There is currently no locale that uses NegativeNumberFormat 4 (n -), so that case cannot be tested. But there's no reason to think it would fail.

                  这篇关于十进制字符串与千位分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:CurrentThread.CurrentUICulture 设置正确,但似乎被 as 下一篇:如何使 RightToLeftLayout 适用于 GroupBoxes 和 Panels 中

                  相关文章

                  最新文章

                • <tfoot id='9dBMg'></tfoot>
                      <bdo id='9dBMg'></bdo><ul id='9dBMg'></ul>

                      <legend id='9dBMg'><style id='9dBMg'><dir id='9dBMg'><q id='9dBMg'></q></dir></style></legend>

                      <small id='9dBMg'></small><noframes id='9dBMg'>

                    1. <i id='9dBMg'><tr id='9dBMg'><dt id='9dBMg'><q id='9dBMg'><span id='9dBMg'><b id='9dBMg'><form id='9dBMg'><ins id='9dBMg'></ins><ul id='9dBMg'></ul><sub id='9dBMg'></sub></form><legend id='9dBMg'></legend><bdo id='9dBMg'><pre id='9dBMg'><center id='9dBMg'></center></pre></bdo></b><th id='9dBMg'></th></span></q></dt></tr></i><div id='9dBMg'><tfoot id='9dBMg'></tfoot><dl id='9dBMg'><fieldset id='9dBMg'></fieldset></dl></div>