Results 1 to 6 of 6

Thread: Force values to two decimal places

  1. #1
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Force values to two decimal places

    I am a very raw beginner. I have the code

    printf('%01.2f', $total);

    but I don't know where to put it. I have a print statement:

    print (
    <p>
    Total<br />
    ' . $total . '
    </p>
    );

    I suppose I change print to printf, is that right? and where do I put the print('#01.2f', $total);

    This probably sounds totally confused but if someone could help sort me out I'd be very grateful.

  2. #2
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone have a suggestion as to where I might go to find an answer or some help towards finding an answer?

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    PHP Code:
    <p>
    Total 
    <?php
    //$total = 1234.93265; for testing
    $total sprintf('%01.2f'$total);
    echo 
    $total;
    ?>
    </p>
    This usage auto rounds, you could use regex to limit it to the single 2 values.

    PHP Code:
    <p>
    Total 
    <?php
    $dec 
    '';
    //$total = '123451234.913412341'; for testing
    if (preg_match('/.*\.(\d\d).*/'$total)) {
        
    $dec '.' preg_replace('/.*\.(\d\d).*/''$1'$total);
        
    $total preg_replace('/(.*)\.\d\d.*/''$1'$total);
    }
    echo 
    $total $dec;
    ?>
    </p>
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Are you trying to display a price with 2 decimal places? If so then you can use the number_format()
    PHP Code:
    number_format($total2); 

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think you could also just use round(). I'm not sure how it differs from number_format() though. Maybe number format can handle more complicated things.
    Code:
    round($number,2);
    Then you can use echo (with anything) to display it. Or print().

    Code:
    echo $variable;
    echo round($number,2);
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much bluewalrus, fastsol1 and djr33. I'll give them all a try and see if I can get something to work.

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
  •