views:

42

answers:

2

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

+8  A: 

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

womp
Ok, this works, but lets say I have the value 1245.2993 and I do C4 as the Format, then it will display 1245.2993, but in this case, I want to just display 1245.29 for example. Is there a way to specify this?
Xaisoft
Format strings aren't conditional - either you want four decimal places or two. You'll have to use conditional logic to check your number beforehand and use the appropriate format string.
womp
string.Format won't do this automatically. If you want different formats depending on the value, you'll have to check the value in code.
Keltex
Ok, I had a feeling it would have to be a code check. Thanks again.
Xaisoft
+3  A: 
string.Format("{0:C4}",....);
Keltex