PHP: How do you convert from EXPONENT_DNUM to LNUM format??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Varius
    Confirmed User
    • Jun 2004
    • 6890

    #1

    PHP: How do you convert from EXPONENT_DNUM to LNUM format??

    I'm having a very annoying problem this morning....

    If I increment a variable by small floats (like 0.00001 lets say) in a loop, the vairable becomes EXPONENT_DNUM format. I would like to keep it in LNUM format. Any ideas?

    I've made a simple loop here to show an example:

    PHP Code:
    <?PHP
    
    $var = 0;
    
    for ($i=0;$i<5;$i++) {
        $var += 0.000004;
    }
    
    echo $var;
    
    ?>
    That gives me "2E-05" instead of the 0.00002 I"m looking for.
    Skype variuscr - Email varius AT gmail
  • Varius
    Confirmed User
    • Jun 2004
    • 6890

    #2
    I do have one way to make it work, but I'd prefer finding out the true method...my way is just a workaround

    I replace the last line by this instead, and then it works:

    PHP Code:
    $num = split("-",$var);
    
    echo number_format($var,$num[1]); 
    
    Skype variuscr - Email varius AT gmail

    Comment

    • pstation
      Confirmed User
      • Jul 2003
      • 1135

      #3
      use the force

      Comment

      • azguy
        Confirmed User
        • Nov 2004
        • 5167

        #4
        Try to add 1 to it

        Comment

        • swedguy
          Confirmed User
          • Jan 2002
          • 7981

          #5
          err......
          Last edited by swedguy; 04-20-2005, 11:40 AM.

          Comment

          • Varius
            Confirmed User
            • Jun 2004
            • 6890

            #6
            Originally posted by azguy
            Try to add 1 to it
            No luck...also tried * 1, using settype to make sure they are equivalent types and few other things.

            The weird part is, if I only add a value once, its fine.

            ie.
            PHP Code:
            $var = 0;
            $var += 0.00004;
            
            echo $var; 
            
            will give me 0.00004 as output.

            Once I loop on it though and add more values, it changes format.

            Weird, no one on IRC or google could solve it...my fix above though works for my purposes..
            Skype variuscr - Email varius AT gmail

            Comment

            • azguy
              Confirmed User
              • Nov 2004
              • 5167

              #7
              Originally posted by Varius
              No luck...also tried * 1, using settype to make sure they are equivalent types and few other things.

              The weird part is, if I only add a value once, its fine.

              ie.
              PHP Code:
              $var = 0;
              $var += 0.00004;
              
              echo $var; 
              
              will give me 0.00004 as output.

              Once I loop on it though and add more values, it changes format.

              Weird, no one on IRC or google could solve it...my fix above though works for my purposes..
              I'd stick to what works then

              Comment

              • jeffs
                Confirmed User
                • Oct 2002
                • 135

                #8
                You can use printf("%f", $var) or printf("%.5f", $var) instead of echo $var.

                Comment

                • mortenb
                  Confirmed User
                  • Jul 2004
                  • 2203

                  #9
                  PHP Code:
                  
                  <?PHP
                  bcscale(6);
                  $var    = 0;
                  
                  for ($i=0;$i<5;$i++) {
                      $var    = bcadd($var,1.000004);
                  }
                  $var    = bcsub($var,5);
                  
                  echo $var;
                  ?>
                  dunno if that is close enough for you..

                  Comment

                  Working...