# Formatting with two decimal places but no rounding in C# Double – ToString()

## Problem

In C#, how can I convert a Double to a String with only two decimal places?

If I use String.Format(“0:0.00 percent “, myDoubleValue), the amount gets rounded, and I just want a straight truncation. I also want the String conversion to be culturally appropriate.

## Solution #1

Here’s what I use:

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

For instance:

If you dial 50.947563 and enter the following, the following will occur:

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

Now that you have your answer trimmed, simply perform the following to format the string:

``````string s = string.Format("{0:N2}%", x); // No fear of rounding and takes the default number format
``````

## Solution #2

Thanks to.##, the numbers are rounded to two decimal places but only show up to two decimal places (removing any trailing zeros).

``````decimal d0 = 24.154m;
decimal d1 = 24.155m;
decimal d2 = 24.1m;
decimal d3 = 24.0m;

d0.ToString("0.##");   //24.15
d1.ToString("0.##");   //24.16 (rounded up)
d2.ToString("0.##");   //24.1
d3.ToString("0.##");   //24
``````

http://dobrzanski.net/2009/05/14/c-decimaltostring-and-how-to-get-rid-of-trailing-zeros/

## Solution #3

I recommend that you truncate first, 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 to truncate two digits; use a 1 followed by as many zeros as you want after the decimal point. To alter the formatting output, use the culture name.

## Solution #4

I make advantage of pricing. For retrieving the leading 0s, use ToString(“0.00”)

``````double total = "43.257"