Reputation: 49
I'm trying to format a toString to one decimal place but it keeps going to something like 21.0445445454. Here's the line of code please let me know what I'm doing wrong
double heightInchToFoot = (12 * Convert.ToDouble(heightFtBox.Text)) +Convert.ToDouble(heightInchBox.Text);
double bmi = (Convert.ToDouble(weightBox.Text) * 703) / (heightInchToFoot * heightInchToFoot);
bmiDisplay.Text = String.Format("{0:0.0}", bmi.ToString());//This is the line of code I'm referring to
Upvotes: 0
Views: 3245
Reputation: 61975
The problem is the use of bmi.ToString()
which converts the double to a string using the default Double.ToString format/rules1. Because of that, the actual Format deals with a string (which doesn't understand decimal formatting) and not the actual double value.
Instead, it should be
String.Format("{0:0.0}", bmi)
or
bmi.ToString("0.0")
1 From the documentation:
The
Double.ToString()
method formats a Double value in the default ("G", or general) format of the current culture.
Upvotes: 5