c00ke
c00ke

Reputation: 2315

Format decimal to string in correct culture info

What is the best way to format a decimal amount to string for UI display in the correct culture info?

Upvotes: 13

Views: 28448

Answers (4)

Derek J.
Derek J.

Reputation: 1460

Also, if you want to use a culture specified by the user you can use:


string userInfo = "en-US";

yourDecimal.ToString("N2", CultureInfo.CreateSpecificCulture(userInfo));

or

yourDecimal.ToString("N2", new CultureInfo(userInfo));

Upvotes: 6

Hans Kesting
Hans Kesting

Reputation: 39339

Add a format to the ToString: myDecimal.ToString("#.00") or myDecimal.ToString("C").

For a nullable decimal (decimal?) you will need to use the .Value property (myNullableDecimal.Value.ToString("C")) or cast the value to a plain (non-nullable) decimal. Be sure not to do this when the value is null or you will get an exception!

See the documentation for Standard or Custom numeric format strings.

Upvotes: 13

Philip Rieck
Philip Rieck

Reputation: 32578

Why not decimalVar.ToString("F2", CultureInfo.CurrentCulture);. For format strings (the "F2" part) and what they do, see Standard Numeric Format Strings and Custom Numeric Format Strings

Upvotes: 8

NetSide
NetSide

Reputation: 3890

use:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);

Upvotes: 6

Related Questions