views:

2639

answers:

3

How can i format a Double to String in c# so as to have only two decimal digits?

If I use String.Format("{0:0.00}%", myDoubleValue) the number is rounded, and i want a simple truncate without rounding. Also, I want the conversion to String to be culture sensitive.

+15  A: 

I use the following:

double x = Math.Truncate(myDoubleValue * 100) / 100;

For instance:

If the number is 50.947563 and you use the following, the following will happen:

- Math.Truncate(50.947563 * 100) / 100;
- Math.Truncate(5094.7563) / 100;
- 5094 / 100
- 50.94

And there's your answer truncated, now to format the string simply do the following:

string s = string.Format("{0:N2}%", x); // No fear of rounding and takes the default number format
Kyle Rozendo
-1 You can do the culture-sensitive formatting in the same `string.Format` step that formats the string. See my answer below.
CesarGon
So you give me a -1 for me saying my answer doesn't do culture sensitive formatting, when in fact, my answer does not do culture sensitive formatting? That's plain silly Cesar.
Kyle Rozendo
Precisely: the poster is asking for a solution that includes culture-sensitive formatting. Your answer is perfectly OK in the truncating part, but, as you well point out, "the culture changing would need to be done separately". You don't specify how to do it, so I think your answer only replies partially to the question.
CesarGon
Alright no problem, I will simply post an addendum.
Kyle Rozendo
That's great. I hope my comment doesn't look that silly now. :-) I'll change my downvote then.
CesarGon
To me it did, as the solution on the string formatting is incredibly simple, the truncation was more tricky.
Kyle Rozendo
If by "culture sensitive" the poster means that the formatting must produce a result that varies depending on a programmer-provided culture value, then your answer still fails there, because it uses the default culture. That's my assumption. But maybe we should ask him/her. My answer below does consider an explicit culture argument that is passed into the formatting code. I know it's simple, but that makes the answer *complete*.
CesarGon
By culture sensitive, I interpreted the opposite, so feel free to ask the op. They obviously got what they needed though.
Kyle Rozendo
Have asked now. I have also removed my downvote. :-)
CesarGon
+8  A: 

I suggest you truncate first, and then format:

double a = 123.4567;
double aTruncated = Math.Truncate(a * 100) / 100;
CultureInfo ci = new CultureInfo("de-DE");
string s = string.Format(ci, "{0:0.00}%", aTruncated);

Use the constant 100 for 2 digits truncate; use a 1 followed by as many zeros as digits after the decimal point you would like. Use the culture name you need to adjust the formatting result.

CesarGon
A: 

You could also write your own IFormatProvider, though I suppose eventually you'd have to think of a way to do the actual truncation.

The .NET Framework also supports custom formatting. This typically involves the creation of a formatting class that implements both IFormatProvider and ICustomFormatter. (msdn)

At least it would be easily reusable.

There is an article about how to implement your own IFormatProvider/ICustomFormatter here at CodeProject. In this case, "extending" an existing numeric format might be the best bet. It doesn't look too hard.

Benny Jobigan