I have a situation where I need to display a double value rounded to two decimal places, but displayed without the decimal. There will be some situations where I will want to use the same code and display the double value differently, so I was hoping I could handle this by passing in a string format pattern.
For example, the double value might be: 11367.2232 I want the value to be: 1136722
Another example, the value might be 344576.3457 I want the value to be 34457635
A third example, the value might be 546788 I want the value to be 54678800
So, I want to do something this:
String.Format("{PATTERN}", dblCurrency);
Is there any formatting pattern that would both round to 2 decimal places and strip the decimal from being displayed?
First, with (very) few exceptions, it's generally bad idea to use double for manipulating currency values. You should really use decimal to represent such amounts.
Second, rounding and display formatting are separate operations, and your code should express them separately. string.Format() does not provide a single format mask that will do both, but you can easily achieve what you're looking for:
decimal amount = 11367.3456m
String.Format( "{0:0}", amount*100 );
which will output:
1136735
The D0 format specifier emits numeric values without any separators and with no digits after the decimal point. You could also just use ToString(), but I think the format specifier conveys the intent more clearly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With