简体   繁体   中英

C# Double - ToString formatting to show a value in cents with no decimal displayed

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.

Use this:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM