简体   繁体   English

对十进制数字C#使用自定义字符串格式

[英]Using custom string format for decimal number c#

I got a decimal values with 4 decimal places in database, but I need to display the format with thousand separator without 0 appears in behind. 我在数据库中得到了一个带有4个小数位的十进制值,但是我需要显示后面带有千位分隔符而没有0的格式。 I have try out G, F, N format but I have no clues on the result that I want. 我已经尝试了G,F,N格式,但是对想要的结果一无所知。

1234.3456          // 1,234.3456
1234.0000          // 1,234.00
1234.3450          // 1,234.345
1234.345678        // 1,234,3457
1234               // 1,234.00
1234.2             // 1,234,20

its very easy here is examples for that problem. 这很容易找到解决该问题的示例。 If its solved your problem, please dont forget to mark as answer :) 如果它解决了您的问题,请不要忘记将其标记为答案:)

 double value;

  value = 123;
  Console.WriteLine(value.ToString("00000"));
  Console.WriteLine(String.Format("{0:00000}", value));
  // Displays 00123

  value = 1.2;
  Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
  Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                    "{0:0.00}", value));
  // Displays 1.20

  Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
  Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                  "{0:00.00}", value));
  // Displays 01.20

  CultureInfo daDK = CultureInfo.CreateSpecificCulture("da-DK");
  Console.WriteLine(value.ToString("00.00", daDK)); 
  Console.WriteLine(String.Format(daDK, "{0:00.00}", value));
  // Displays 01,20

  value = .56;
  Console.WriteLine(value.ToString("0.0", CultureInfo.InvariantCulture));
  Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                  "{0:0.0}", value));
  // Displays 0.6

  value = 1234567890;
  Console.WriteLine(value.ToString("0,0", CultureInfo.InvariantCulture));   
  Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                  "{0:0,0}", value));   
  // Displays 1,234,567,890      

  CultureInfo elGR = CultureInfo.CreateSpecificCulture("el-GR");
  Console.WriteLine(value.ToString("0,0", elGR));   
 Console.WriteLine(String.Format(elGR, "{0:0,0}", value));  
  // Displays 1.234.567.890

  value = 1234567890.123456;
  Console.WriteLine(value.ToString("0,0.0", CultureInfo.InvariantCulture)); 
  Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                  "{0:0,0.0}", value)); 
  // Displays 1,234,567,890.1  

  value = 1234.567890;
  Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture));    
  Console.WriteLine(String.Format(CultureInfo.InvariantCulture, 
                                  "{0:0,0.00}", value));    
  // Displays 1,234.57 

Actually, the MSDN page for Decimal.ToString has almost exacly what you need in their last example. 实际上, Decimal.ToString的MSDN页面几乎可以满足您在上一个示例中的需要。

specifier = "#,#.00#;(#,#.00#)" 说明符=“#,#。00#;(#,#。00#)”

So just deleting the negative format, and adding an extra # at the end done it: 因此,只需删除否定格式,并在末尾添加一个额外的#
specifier = "#,#.00##"

Just tried it with: 刚刚尝试使用:

decimal[] decimals = {1234.3456M, 1234.0000M, 1234.3450M, 1234.345678M, 1234, 1234.2M};
string[] formattedDecimals = decimals.Select(d => d.ToString(specifier)).ToArray();

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

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