2

Let's say we have 12.054 and I want to split it to three variables like $whole_number=12 $numerator=54 and $denominator=1000. Could you help me?

Ricky Bobby
  • 7,490
  • 7
  • 46
  • 63
Templar
  • 1,843
  • 7
  • 29
  • 42
  • What's the purpose of this? How do you want to use them? – benqus Jun 29 '11 at 09:35
  • You probably would never actually *have* such a number because 54/100 is not a sum of powers of two. So at best you could put in an arbitrary round-off and get some approximate answer. Or more simply put, if `$x = 1.0/3.0;`, do you want 33/100, 333/1000 or 3333/10000? – Kerrek SB Jun 29 '11 at 10:13
  • @Kerrek SB I enter number manually through html form – Templar Jun 29 '11 at 10:46
  • Related: https://stackoverflow.com/q/6619377/2943403 – mickmackusa Oct 21 '21 at 02:31

3 Answers3

7

A straight-forward approach - not very academic, but it works for PHP ;-):

$float        = 12.054;
$parts        = explode('.', (string)$float);
$whole_number = $parts[0];
$numerator    = trim($parts[1], '0');
$denominator  = pow(10, strlen(rtrim($parts[1], '0')));

Some more work might be needed to ensure that edge case work too (trailing 0s, no decimal part at all, etc.).

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • If you might reach the integer limit, then just remove the `(int)`-cast, which is not really necessary. `$whole_number` and `$numerator` will be strings then. – Stefan Gehrig Jun 29 '11 at 09:42
  • If I don't use int then $numerator will be 054, how could I remove those zero(s)? – Templar Jun 29 '11 at 10:37
  • Edited the example - now using `trim()` to remove training and leading `0`s from the `$numerator` – Stefan Gehrig Jun 29 '11 at 10:43
  • would it be possible to get from $float = 1/3 $numerator = 1 and $denominator = 3 (of coure not with this code) or no because of precision? – Templar Jun 30 '11 at 08:46
4

Here is something to get you started , based on simple type conversions.

http://codepad.org/7ExBhTMS

However, there are many cases to consider like :

  1. Preceding/trailing zeros. 12.0540 ( is 540/10000 or 54/1000 for you )

  2. Handling decimals with no fractional part eg. 12.00 .

    $val = 12.054;
    print_r(splitter($val));
    
    function splitter($val)
    {
      $str = (string) $val ;
      $splitted = explode(".",$str);
      $whole = (integer)$splitted[0] ;
      $num = (integer) $splitted[1];
      $den = (integer)  pow(10,strlen($splitted[1]));
      return array('whole' => $whole, 'num' => $num,'den' => $den);
    }
    
    
    
    ?>
    
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
2

Try This

<?
$no = 12.54;
$arr = explode(".", $no);

$full_no = $arr[0].$arr[1];

for($i = 0; $i < strlen($arr[1]); $i++) $denominator = $denominator."0";

$denominator = "1".$denominator;

$numerator = $full_no % $denominator;

$whole_no = $full_no / $denominator;

echo "Denominator = ". $denominator ."<br>";

echo "Numerator = ". $numerator ."<br>";

echo "Whole No = ". (int)$whole_no ."<br>";
?>

Output :

Denominator = 100
Numerator = 54
Whole No = 12

Output for 12.054 :

Denominator = 1000
Numerator = 54
Whole No = 12

I hope this will help you..

Gaurav Porwal
  • 503
  • 3
  • 6