开发者

C# sometimes currency format does not work

It seems that sometimes the currency format does not work:

string Amount = "11123.45";
Literal2.Text = stri开发者_开发百科ng.Format("{0:c}", Amount);

reads 11123.45

it should be:

$11,123.45


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.)


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


This worked for my situation

string Amount = "11123.45";
amount2 = amount.AsDecimal();
string.Format("{0:c}", @amount2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜