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?
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 %.
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.
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.
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
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With