Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9

Thread: floats in PHP

  1. #1
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    PHP Code:
    $a 5.01;
    $b 5.00;

    echo 
    $a."<br>".$b
    outputs:
    5.01
    5

    How can I get $b to print out 5.00?

  2.  

  3. #2
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    Have you tried
    PHP Code:
    <?php
    $a 
    5.01
    $b 5.00

    echo 
    $a."<br>";
    printf($b);
    ?>

  4. #3
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    yip. It still only prints 5


  5. #4
    Senior Member Eddy Bones's Avatar
    Join Date
    Jan 2004
    Location
    Washington, USA
    Posts
    1,054
    Member #
    4651
    PHP Code:
    $a 5.01;
    $b "5.00";

    echo 
    $a."<br>".$b

  6. #5
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    I'm pretty sure that the trouble you are having is due to the fact that PHP converted your "5.0" to a string when it concatinated it with "<br>".
    use printf()/sprintf() functions to properly format your output.

    PHP Code:
    $a 5.01;
    $b = (float)5.00;

    echo 
    $a."<br>".sprintf("%f",$b);

    // OR

    echo $a "<br>";
    printf("%f"$b); 
    There and Back Again :Ogre:

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    It's a formatting issue.
    If you use numbers like 5.000000 they are still 5.
    A number like 5.000001 will print like 5.000001 because there's a value behind the decimal.

    If you want to format your output to always include 2 digits behind the 0 wether there's a value there or not:

    $
    PHP Code:
    5.01;
    $b 5.000;
    $b=sprintf("%01.2f"$b);
    echo 
    $a." : ".$b
    S. Rosland

  8. #7
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    what he said too.
    There and Back Again :Ogre:

  9. #8
    Senior Member Stylise's Avatar
    Join Date
    Jul 2005
    Location
    Mount Martha, Australia
    Posts
    229
    Member #
    10679
    I usually use format_number, the syntax being:

    number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] )

    However, it will return a String, so if you'd want it to be a float, you'd have to cast it back.

  10. #9
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    thanks rosland and nsr81. the sprintf() function works!

    :classic:


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 06:21 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com