Formatting Problem

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

    Formatting Problem

    In a program I'm creating, I'm trying to calculate a percentage. However,
    for some reason the calculation is only coming out to 1 decimal place, and I
    know there's more. Using this code:

    $gshare = $shareresults / $phenresults;
    echo "$shareresu lts / $phenresults = $gshare";

    I get the following output:

    3,340,000 / 5,180,000 = 0.6

    That's wrong. How can I correct this?


  • Andy Hassall

    #2
    Re: Formatting Problem

    On Sat, 14 Oct 2006 17:58:26 -0400, "Smiley" <none@ofyourbus iness.comwrote:
    >In a program I'm creating, I'm trying to calculate a percentage. However,
    >for some reason the calculation is only coming out to 1 decimal place, and I
    >know there's more. Using this code:
    >
    >$gshare = $shareresults / $phenresults;
    >echo "$shareresu lts / $phenresults = $gshare";
    >
    >I get the following output:
    >
    >3,340,000 / 5,180,000 = 0.6
    They're not numbers, not with the commas in them. You can't divide formatted
    strings.

    When asked to treat strings as numbers, PHP reads as much of the data until it
    stops matching basic numeric format, so you're actually getting 3/5 which is
    0.6.

    Remove the commas; only add them back in again when formatting them for
    display.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • ruibalp@gmail.com

      #3
      Re: Formatting Problem

      if you're really lazy:

      $gshare = str_replace(',' ,'',$shareresul ts) /
      str_replace(',' ,'',$phenresult s);

      Comment

      Working...