getting fraction from decimal number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bacci

    #1

    getting fraction from decimal number

    This isn't necessarily a php-specific question, but I'm looking for a
    formula to determine the fraction portion of a decimal number. For instance,
    8.25. I could simply search the number (as a string) and return everything
    from the right of the decimal point (.25). However is there a formula to do
    this more eloquently?

    Thanks in advance.


  • Pedro Graca

    #2
    Re: getting fraction from decimal number

    Bacci wrote:[color=blue]
    > This isn't necessarily a php-specific question, but I'm looking for a
    > formula to determine the fraction portion of a decimal number. For instance,
    > 8.25. I could simply search the number (as a string) and return everything
    > from the right of the decimal point (.25). However is there a formula to do
    > this more eloquently?[/color]

    Maybe

    <?php
    $x = 8.25;
    $y = $x - (int)$x; // $y now has 0.25
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • FLEB

      #3
      Re: getting fraction from decimal number

      Regarding this well-known quote, often attributed to Pedro Graca's famous
      "8 Feb 2004 12:20:43 GMT" speech:
      [color=blue]
      > Bacci wrote:[color=green]
      >> This isn't necessarily a php-specific question, but I'm looking for a
      >> formula to determine the fraction portion of a decimal number. For instance,
      >> 8.25. I could simply search the number (as a string) and return everything
      >> from the right of the decimal point (.25). However is there a formula to do
      >> this more eloquently?[/color]
      >
      > Maybe
      >
      > <?php
      > $x = 8.25;
      > $y = $x - (int)$x; // $y now has 0.25
      > ?>[/color]

      I'd recommend using:
      $y = $x - floor($x);
      to be sure of the rounding method.

      --
      -- Rudy Fleminger
      -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
      (put "Hey!" in the Subject line for priority processing!)
      -- http://www.pixelsaredead.com

      Comment

      Working...