SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a way to always have decimal numbers, even if they are whole

    Is there a way with the number_format function to force decimal places even if the number comes out whole like 5 or 10? Its basically for adding up money values and obviously when it is 5.42, it shows up as 5.42 but a 5.00 only shows up as 5. Same thing goes for a 5.10 displaying as 5.1

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    number_format should do it
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using number format but maybe I am doing it wrong? The $subtotal could be any number from 5 to 12.3 to 40.52 obviously. I just want to make sure that it at least has two numbers following the decimal place. Here is what I have.

    PHP Code:
    <?php echo number_format($subtotal2'.'','); ?>
    and for some math:

    PHP Code:
    <?php
    $month12 
    round(number_format($subtotal2'.'',') * (0.25), 2);
    echo 
    "$month12 per month";
    ?>

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Apply number_format to the result of calculations, not to the numbers going into the calculations.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use
    PHP Code:
    echo sprintf('%.2f'5); 

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    The money_format function may make more sense for this particular question.
    Salathe
    Software Developer and PHP Manual Author.


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
  •