Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String format for only one decimal place?

Tags:

c#

.net

I'd like to dispaly only one decimal place. I've tried the following:

string thevalue = "6.33"; thevalue = string.Format("{0:0.#}", thevalue); 

result: 6.33. But should be 6.3? Even 0.0 does not work. What am I doing wrong?

like image 589
4thSpace Avatar asked Aug 27 '12 16:08

4thSpace


People also ask

How do you format a string to show two decimal places?

String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

What is .2f in Java?

we now see that the format specifier "%. 2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places. Similarly, had we used "%. 3f", x would have been printed rounded to 3 decimal places.

What is a single decimal place?

One decimal place to the left of the decimal point is the ones place. One decimal place to the right of the decimal place is the tenths place. Keep your eye on the 9 to see where the decimal places fall.


1 Answers

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.)

like image 131
Ry- Avatar answered Sep 22 '22 17:09

Ry-