简体   繁体   中英

Putting Commas in a Decimal

IF i have the following:

MyString.ValueType = typeof(System.Decimal);

how can i make this have an output of decimals with commas? In other words, instead of seeing 1234.5, i'd like to see 1,234.5

Use:

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

The "N" format specifier will put in the thousands separators (,). For details, see Decimal.ToString(string) .

Edit:

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

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

That will force it to use the InvariantCulture, which uses comma for thousands and period for decimal separation, which means you'll always see "1,234.5".

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