2

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?

1
  • In the example, I should have named the variable "dblValue" instead of referring to it as currency. It doesn't represent currency.
    – R J
    Jul 28, 2010 at 19:45

4 Answers 4

19

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.

5
  • 1
    If I could do +2 I would, specifically for the comment about decimal vs double.
    – user114600
    Jul 28, 2010 at 19:09
  • The format specifier already rounds, you don't need Math.Round(). Jul 28, 2010 at 19:12
  • I get an error with this code. "FormatException" error. "Format specifier was invalid."
    – R J
    Jul 28, 2010 at 19:27
  • @rj8: Yes, my mistaked. The D format specifier can only be used with integral types. It's actually enought to use {0:0} as the format; in which case the Math.Round() call is unnecessary (as Hans stated). I've updated my sample code.
    – LBushkin
    Jul 28, 2010 at 20:06
  • I suppose that is as good as I am going to get. I agree on decimal vs double, but this isn't really for currency. I was thinking currency, because I was hoping .NET might have something built-in for handling a scenario where you want to express a decimal value in total number of cents. It just seemed like it might be a common scenario. Anyway, for this function, I will just have to deal with parameters for both steps; rounding and multiplying.
    – R J
    Jul 28, 2010 at 22:07
0

Use this:

var dblCurrency = 11367.2232D;
var rounded = Math.Round(dblCurrency, 2);
var cents = rounded*100;
var centString = cents.ToString();
0

You could use a custom formatting function like this:

string ScaledFormat(string format, double value, double scaleFactor)
{
    return(string.Format(format, Math.Round(value * scalefactor, 0)));
}

or, if you only have a few format styles, I'd use an enum:

enum CustomFormat { Integer, IntegerX100 };
string ScaledFormat(CustomFormat format, double value)
{
    switch(format)
    {
        case CustomFormat.Integer:     return(string.Format("{0}", (int) value);
        case CustomFormat.IntegerX100: return(string.Format("{0}", Math.Round(value * 100.0, 0));
    }
}

This means there are a distinct number of ways the value can be formatted, and the actual conversion is totally encapsulated in the method, ensuring consistent output throughout your program, and avoiding scattering "magic constants" everywhere. It also centralises the formatting so you can easily adjust it in future without having to find and correct hundreds of different string.Format calls, and reduces the testing necessary because you're just re-using a small piece of well-tested code. And it makes the cases where you are formatting the string much easier to read/understand (self documenting code).

0

To get what you need done try something like:

decimal dNumber = 11367.2232;
String.Format( "{0:0}", Math.Round(amount, 2) * 100));

This should output 1136722.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.