Issue
The following code keeps producing 5ft 11.8in instead of 4ft 11.8in when I type in 152 cm. I thought it was the floating point inaccuracies at first so I tried using decimal but that also gives me the same value. Is there anyway to make it more accurate? The number in height before it is divided by 12 is 59.84251....
class Height
{
private decimal height;
private string unit;
public static string ConvertedHeight(decimal height, string unit)
{
if (unit == "m")
{
height = height * 100 / 2.54m;
string conversion = (height / 12).ToString("F0") + "ft " + (height % 12).ToString("F1") + "in/n";
return conversion;
}
else if (unit == "cm")
{
height = height / 2.54m;
string conversion = (height / 12).ToString("F0") + "ft " + (height % 12).ToString("F1") + "in/n";
return conversion;
Solution
You need cast your feet conversation to int
in this case.
This is the code you need for division by 12.
(int) (height / 12)
Hope this helps!
Answered By - dj079 Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.