开发者

Format a value that is under 1.00 with {0:C}?

If I have the value 0.0042 returned and I pass this to:

string.Format("{0:C}",....);

It displays $0 as the results 开发者_如何学运维when I want it to actually display:

$0.0042


"C" is the default currency format string, which will always truncate your number to two decimal places. You need to specify the number of decimal places if you're dealing with tiny fractions of a cent.

Try

string.Format("{0:C4}",....);

More currency formatting options can be found here.


string.Format("{0:C4}",....);


Pardon the question necromancy, but for anyone reading this today, here's a custom format string we use. It matches the standard "C" format for U.S. currency, except that it will optionally show a third or fourth decimal place if present. This should work great for formatting a SQL Server money value with fractions of a cent.

// String.Format() example
string.Format("{0:$#,0.00##;($#,0.00##);$\0\.\0\0}", someDecimalValue)
// Decimal.ToString() example
someDecimalValue.ToString("$#,0.00##;($#,0.00##);$\0\.\0\0")

Because it's not using the standard "C" format, it's not sensitive to locale. Extra consideration should be given for applications that require internationalization.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜