Decimal places

IF I have the following:

MyString.ValueType = typeof(System.Decimal); 

how can i do this with the output of decimal places with commas? In other words, instead of seeing 1234.5, I would like to see 1,234.5

+7
c #
source share
1 answer

Using:

 String output = MyString.ValueType.ToString("N"); 

The format specifier "N" will be placed in thousands separators (,). See Decimal.ToString (string) for more details.

Edit:

The above will use your current culture settings, so thousands separators will depend on the current locale. However, if you want it to always use a comma and period for the decimal separator, you can do:

 String output = MyString.ValueType.ToString("N", CultureInfo.InvariantCulture); 

This will force him to use the InvariantCulture, which uses a comma for thousands and a period for decimal separation, which means you will always see โ€œ1,234.5โ€.

+16
source share

All Articles