Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to format a double to one decimal place without rounding

Tags:

I need to format a double value to one decimal place without it rounding.

double value = 3.984568438706 string result = ""; 

What I have tried is:

1)

result = value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture) + "%";  // returns 3.98% 

2)

result = value.ToString("##.#", System.Globalization.CultureInfo.InvariantCulture) + "%";  // returns 4% 

3)

 result = value.ToString("##.0", System.Globalization.CultureInfo.InvariantCulture) + "%";   // returns 4.0% 

4) (Following other suggestions)

value = (value / 100); result = String.Format("{0:P1}", Math.Truncate(value * 10000) / 10000); // returns 4.0%  result = string.Format("{0:0.0%}",value); // returns 4.0% 

What I need to display is the value 3.9%

Thanks for any help in advance.

like image 418
SMULLER Avatar asked Jul 31 '12 15:07

SMULLER


Video Answer


1 Answers

result=string.Format("{0:0.0}",Math.Truncate(value*10)/10); 
like image 158
Bob Vale Avatar answered Oct 16 '22 00:10

Bob Vale