adding numbers in php

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

    adding numbers in php

    Hi,

    I have the following situation:

    $a=5.6000;
    $b=8.9000;
    $c=($a+$b);

    echo $c;

    I will get the value '14.5' back from PHP.
    Wich is logical.
    But what I want is to get the value '14.5000'.
    Is there somehow a workaround to let PHP print 4 decimals?

    Thanks in advance for your reaktion.

    Sander Peters
    The Netherlands


  • Ewoud Dronkert

    #2
    Re: adding numbers in php

    Sander Peters wrote:[color=blue]
    > Is there somehow a workaround to let PHP print 4 decimals?[/color]

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    --
    E. Dronkert

    Comment

    • Justin Koivisto

      #3
      Re: adding numbers in php

      Sander Peters wrote:[color=blue]
      > Hi,
      >
      > I have the following situation:
      >
      > $a=5.6000;
      > $b=8.9000;
      > $c=($a+$b);
      >
      > echo $c;
      >
      > I will get the value '14.5' back from PHP.
      > Wich is logical.
      > But what I want is to get the value '14.5000'.
      > Is there somehow a workaround to let PHP print 4 decimals?
      >
      > Thanks in advance for your reaktion.
      >
      > Sander Peters
      > The Netherlands
      >
      >[/color]

      printf('%.4f',$ c)

      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Tim Roberts

        #4
        Re: adding numbers in php

        "Sander Peters" <sanderpeters@y ahoo.com> wrote:[color=blue]
        >
        >I have the following situation:
        >
        >$a=5.6000;
        >$b=8.9000;
        >$c=($a+$b);
        >
        >echo $c;
        >
        >I will get the value '14.5' back from PHP.[/color]

        Maybe. I'm sure you will shortly be coming up against the NEXT surprising
        truth about floating point values, and that is that 5.6 and 8.9 cannot be
        represented exactly in binary. Just like trying to represent 1/3 in
        decimal, values like 5.6 and 8.9 requires an infinite number of bits. So,
        the value you actually get is only an approximation.

        Because of that, 5.6 + 8.9 does not actually equal 14.5 (which CAN be
        represented exactly in binary). It will PRINT as 14.5, because printing
        rounds it, but if you do this:

        if( $c == 14.5 )
        echo "yes"

        it will not actually print "yes".
        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • Sander Peters

          #5
          Re: adding numbers in php


          "Justin Koivisto" <justin@koivi.c om> schreef in bericht
          news:aOOdnbMi-5XIF8LeRVn-iQ@onvoy.com...[color=blue]
          > Sander Peters wrote:[color=green]
          >> Hi,
          >>
          >> I have the following situation:
          >>
          >> $a=5.6000;
          >> $b=8.9000;
          >> $c=($a+$b);
          >>
          >> echo $c;
          >>
          >> I will get the value '14.5' back from PHP.
          >> Wich is logical.
          >> But what I want is to get the value '14.5000'.
          >> Is there somehow a workaround to let PHP print 4 decimals?
          >>
          >> Thanks in advance for your reaktion.
          >>
          >> Sander Peters
          >> The Netherlands[/color]
          >
          > printf('%.4f',$ c)
          >
          > --
          > Justin Koivisto, ZCE - justin@koivi.co m
          > http://koivi.com[/color]

          Thanks a lot!


          Comment

          Working...