SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: decimal places

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    decimal places

    Hi all,

    I have created a very basic database which records my appointments against my purchases.

    I want to know what my conversion is, to that end i am using the following, however I want it to only display the result to 2 decimal places.


    Code:
    $conversion = $purchases_total / $appts * 100;
    $purchases_total is made up of adding 2 variables together, these variables get the info direct from the database. $appts is simply a count of the total entries.



    Any assistance in this would be greatly appreciated.

    I cant seem to find anything on google or within the forum which fits the bill. I am guessing its something simple but am stuck.

    many Thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Googling for php decimal places gave me this: http://php.net/manual/en/function.number-format.php
    Does that solve your problem?

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guido,

    I am sure that does solve my problem. I did find that page myself but I don't understand what is telling me to do, or where to put the formatting.

    Thats why I came here, usually get things explained in a more simpler fashion so I understand what to do.

    Thanks

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,214
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    You will want to use it like so:
    PHP Code:
    $outputTotal number_format($conversion2); 
    If you need to change the thousands separator or the decimal separator (due to not working in US currency, or a foreign currency that uses a similar definition) then be sure to supply those additional parameters
    PHP Code:
    $outputTotal number_format($conversion2',''.'); 

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cpradio.

    Many thanks, the bit I wasn't getting was creating another variable, now I see the answer I can see it in the examples at the site above.

    Sometimes I get a bit bogged down with the jargon and I am not sure what I am putting where.

    As always very grateful for your assistance

    many Thanks

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Number_format returns a string (note: You can see that in the manual page - the return type is listed in the function definition before the function name.), so it's also perfectly acceptable to call this function amid an echo with appropriate concatenation;

    echo "My number is ".number_format($somenumber,2)." so there."
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The two methods through which you can sort your query is

    1) return number_format((float)$number, 2, '.', '');

    2) $padded = sprintf('%0.2f', $unpadded);

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is the OP talking about money? money_format()


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •