1

I have used this code which shows an error:ie if my meters is 175m but when i convert it does'nt show that thing

<?php
function metersToFeetInches($meters, $echo = true)
{
    $m = $meters;
    $valInFeet = $m*3.2808399;
    $valFeet = (int)$valInFeet;
    $valInches = round(($valInFeet-$valFeet)*12);
    $data = $valFeet."&prime;".$valInches."&Prime;";
    if($echo == true)
    {
            echo $data;
    } else {
            return $data;
    }
}
?>
<?php
$feetInches = metersToFeetInches(1.75,false);
echo $feetInches;
?>
pstrjds
  • 16,840
  • 6
  • 52
  • 61
ede
  • 83
  • 4
  • 8
  • 7
    Well... care to share the error message? – PeeHaa Aug 20 '12 at 20:29
  • 1
    I ran your code and got `5′9″` which sounds roughly correct (5 feet, 9 inches) – pstrjds Aug 20 '12 at 20:33
  • 2
    [This](http://stackoverflow.com/questions/3726721/php-math-precision) may help understand why your results are different from what you expect. – Matt Aug 20 '12 at 20:34
  • I'm getting the same thing as @pstrjds... which is why you should show us your error message. :) – Phil Aug 20 '12 at 20:36
  • @PeeHaa 5.9 is the output, but absolute is 5.74 – ede Aug 20 '12 at 20:37
  • @pstrjds 5.9 is wrong , 5.74 is correct, easy way to check the answer in google – ede Aug 20 '12 at 20:38
  • @flep 5.74 is the right answer – ede Aug 20 '12 at 20:39
  • 1
    @ede - I didn't get 5.9, I got 5' 9", the floating point value is 5.74..., you are taking 5.74... - 5, then multiplying by 12 which gives you `8.8976379`, you then round that. The correct rounded to nearest int value of 8.89... is 9. You are getting what you coded. – pstrjds Aug 20 '12 at 20:45
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – miken32 Nov 30 '18 at 00:06

2 Answers2

4

This is one of those things you can easily find on Google, but I guess you didn't look further than the first link.

Anyways, you should do it like this.

<?php

function metersToFeet($meters) {
    return floatval($meters) * 3.2808399;
}

?>

But why is this better than the code you posted? Well, functions are not supposed to do everything. You should write a function for a certain action, not one big function with everything you can every need. Because if you need to add something to that function or change the order of actions, it's a hell of a lot of work.

Furthermore, your function has an option $echo. But why would you need that? You can add such an option to every function, but PHP has a nice commando for that: echo. So instead, it's way better to write echo metersToFeet(10). Or $value = metersToFeet(10) if you need to save the result in a variable.

Frog
  • 1,631
  • 2
  • 17
  • 26
2
<?php
function metersToFeetInches($meters, $echo = true)
{
    $m = $meters;
    $valInFeet = $m*3.2808399;
    $valFeet = (int)$valInFeet;
    $valInches = round(($valInFeet-$valFeet)*12);
    $data = $valFeet."&prime;".$valInches."&Prime;";
    if($echo == true)
    {
        echo $data;
    } else {
        return $data;
    }
}
?>
Sandeep Sherpur
  • 2,418
  • 25
  • 27