开发者

Conversion and rounding help

I need to convert pounds to开发者_开发百科 kilograms and vice versa -- and round the number to the nearest quarter (and possibly half). I need to be able to make a conversion, take that conversion and convert it back, and have all the values still be the same.

Sample code:

for ($i = 1; $i <= 100; $i = $i + .25)
{
    $kilograms     = convert_pounds_to_kilograms($i);
    $pounds        = convert_kilograms_to_pounds($kilograms);
    $new_kilograms = convert_pounds_to_kilograms($pounds);

    echo ("$i => $pounds => $kilograms => $new_kilograms<br/>");
}

function convert_pounds_to_kilograms($pounds)
{
    assert(is_numeric($pounds) === TRUE);

    $kilograms = $pounds * 0.45359237;

    // Round to the nearest quarter
    $kilograms = round($kilograms * 4, 0) / 4;

    return $kilograms;
}

function convert_kilograms_to_pounds($kilograms)
{
    assert(is_numeric($kilograms) === TRUE);

    $pounds = $kilograms * 2.20462262185;

    // Round to the nearest quarter
    $pounds = round($pounds * 4, 0) / 4;

    return $pounds;
}

The first line of output is correct:

1 => 1 => 0.5 => 0.5

The second is not correct:

1.25 => 1 => 0.5 => 0.5

(the value 1 should have been 1.25)

How do I do this? I'm not looking for precision in the conversion, obviously. I just need to be able to convert these imprecise values back and forth to the same number.

EDIT 1:

The reason for this is that I will be allowing users to enter their height in centimeters, meters, or feet/inches -- then saving whatever their entered value to centimeters (thus, the first conversion). Users can then view their height in either centimeters, meters, or feet/inches (thus, a possible conversion again).

So, say a user enters their height in ft/inches, I need to store that in centimeters. Then the user may want to see that height again in ft/inches -- meaning I need to convert the centimeters back to the original ft/inches value.

Users will probably be limited to entering and viewing values to quarter increments. Meaning, 5'8.25" is valid, but not 5'8.39".


Do not round in the function itself. Go as precise as you can. Only round right before you display it.

If you round it off in the functions, then the ROUNDED value is put into the next function. If you keep doing this, you're going to lose a lot of precision, and you'll get less precise results the more you loop it.


You are rounding to 0 decimal places, hence the 1.25 is rounded to 1. Try removimg the round() function and see what happens.

http://php.net/manual/en/function.round.php

Edit

To address your comment change:-

$kilograms = convert_pounds_to_kilograms($i);

To:-

$kilograms = round(convert_pounds_to_kilograms($i), 0);

And remove round() from inside your functions.


You're rounding out the precision. Your $pounds that you're printing out are converted from original value ($i) to kilograms with a round function, and then back to pounds with a round function; the round() is causing your values to converge.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜