简体   繁体   中英

c# - How do I round a decimal value to 2 decimal places. with , every 1000

So always add a comma and have it to two decimal places, "F" nearly works but can't find the right solution

decimal = 1000.5

test.Text = decimal.ToString("F")

I've also tried:

String.Format("{0:#,###.##}", decimal);

I want to display as the string as 1,000.50

Try:

String.Format("{0:#,###.00}", decimalNumber);

See: Custom Numeric Format Strings

0 - Zero placeholder Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string. More information: The "0" Custom Specifier .

It is not going to round the numbers, it is just string formatting.

For culture insensitive formatting do:

String.Format(CultureInfo.InvariantCulture, "{0:#,###.00}", decimalNmber);
String.Format(CultureInfo.InvariantCulture, "{0:0,0.00}", decimal)

For more options see this link .

And here you can test this online.

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