Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# sometimes currency format does not work

Tags:

c#

asp.net

It seems that sometimes the currency format does not work:

string Amount = "11123.45";
Literal2.Text = string.Format("{0:c}", Amount);

reads 11123.45

it should be:

$11,123.45

like image 304
aron Avatar asked May 02 '26 15:05

aron


2 Answers

That code would never work - because Amount is a string, not a number. The currency format only applies to numbers.

For example:

decimal amount = 11123.45m;
Console.WriteLine(string.Format("{0:c}", amount);

(Note that using double for currencies is almost always a bad idea, as double can't exactly represent many decimal numbers. Decimal is a more appropriate type for financial data.)

like image 90
Jon Skeet Avatar answered May 04 '26 03:05

Jon Skeet


It's because Amount is a string instead of a numeric.

like image 44
BC. Avatar answered May 04 '26 03:05

BC.