Solved

how do I do this with numbers in a string?

Posted on 2004-10-24
184 Views
Last Modified: 2008-07-03
I have a Float say 25.4812756

When I dump it out, I want to limit my output to say, 25.48.  how can I limit the number of decimal places that get outputted into a string?

Thanks!
0
Question by:will1383
    8 Comments
     
    LVL 7

    Expert Comment

    by:BenMorel
    Hello,

    You should use printf (or sprintf in this example) for this purpose.

    <?php
    $float_number = 25.4812756;
    $string = sprintf('%01.2f', $float_number);
    echo $string; // 25.48
    ?>

    See ya ;)
    Ben
    0
     
    LVL 27

    Accepted Solution

    by:
    Use the round function, its what its designed for. printf is not needed here.

    <?php
    $var = 25.4812756;
    echo round($var,2);
    ?>

    http://us2.php.net/manual/en/function.round.php
    0
     

    Author Comment

    by:will1383
    Oh yea,

    Just like C.  Doh!
    0
     
    LVL 12

    Expert Comment

    by:minichicken
    Hi

    You can also use number_format(); to do more formating to you output.

    >>number_format ( float number, int decimals, string dec_point, string thousands_sep)
    http://za2.php.net/number_format

    e.g

    $number = 1234.5678;

    echo number_format($number, 2, '.', ',');

    //outputs // 1,234.57

    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    I agree Diablo ;)

    The difference is that round($n,$max) outputs $n with $max MAXIMUM decimals.
    printf() here has a fixed number of decimals, which is better for printing a price, for example.
    Depends on what it is used for!

    Ben
    0
     

    Author Comment

    by:will1383
    oh man.

    sorry about pulling the trigger with the accepted answer.  I didn't realize there was this many ways to handle number formatting in php.  Thank you ALL!!!
    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    No problem Will ;)
    0
     
    LVL 12

    Expert Comment

    by:minichicken
    No Prob...

    I answered the question too slow anyways..... :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Suggested Solutions

    Title # Comments Views Activity
    strange $_SESSION behaivor 43 45
    Android Box Xibo software 3 34
    Automatic Image Uploading 9 30
    MySQL tables for vtiger CRM 4 12
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now