kjv kjv - 7 months ago 72
C# Question

C# Double - ToString() formatting with two decimal places but no rounding

How do I format a

to a
in C# so as to have only two decimal places?

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


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