0

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
1
  • I don't know if I'm right but have you tried ############.# format?? Commented Jul 13, 2014 at 2:13

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.