String format for only one decimal place?

You need it to be a floating-point value for that to work.

double thevalue = 6.33;

Here's a demo. Right now, it's just a string, so it'll be inserted as-is. If you need to parse it, use double.Parse or double.TryParse. (Or float, or decimal.)


Here is another way to format floating point numbers as you need it:

string.Format("{0:F1}",6.33);

Here are a few different examples to consider:

double l_value = 6;
string result= string.Format("{0:0.00}", l_value );
Console.WriteLine(result);

Output : 6.00

double l_value = 6.33333;
string result= string.Format("{0:0.00}", l_value );
Console.WriteLine(result);

Output : 6.33

double l_value = 6.4567;
string result = string.Format("{0:0.00}", l_value);
Console.WriteLine(result);

Output : 6.46

Tags:

C#

.Net