简体   繁体   中英

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. I have try out G, F, N format but I have no clues on the result that I want.

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.

specifier = "#,#.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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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