Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    Round of into two decimal places

    Hi..

    I encountered problem in rounding of numbers into two decimal places.

    here is my sample code:

    PHP Code:
    if($W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
       
    $TAX = ($TotEarn 7917);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX number_format($TAX2'.''');

    for example from this:
    PHP Code:
     $TAX = ($TotEarn 7917);
      
    $TAX = (937.50 + ($TAX .25)); 
    the output is: 1417.615

    using this:
    PHP Code:
    $TAX number_format($TAX2'.'''); 
    the output was : 1417.61

    but it should be : 1417.62

    Thank you

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    $t 1417.615;
    print 
    number_format($t2); 
    I got 1417.62 when given 1417.615.
    You sure your calculation actually adds up to 1417.615?

  • #3
    New Coder
    Join Date
    Apr 2012
    Location
    Surat
    Posts
    34
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Hi,

    if u r getting value like this 1417.61 and display it using number_format then u will get the result same but,
    if u r getting value like this 1417.615 and display it using number_format then u will get the result like 1417.62

    so kindly check the decimal points u getting.

    Regards,
    mistry Sandip

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    $t 1417.615;
    print 
    number_format($t2); 
    I got 1417.62 when given 1417.615.
    You sure your calculation actually adds up to 1417.615?
    Yes... I'm sure

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by newphpcoder View Post
    Yes... I'm sure
    PHP Code:
    printf('%0.5f; %s'$TAXnumber_format($TAX2'.'',')); 
    What are the results?

    Edit:
    Make sure that is done before your number_format.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    printf('%0.5f; %s'$TAXnumber_format($TAX2'.'',')); 
    What are the results?

    Edit:
    Make sure that is done before your number_format.
    the result is :

    1417.61500; 1,417.61

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Code:
    >php -r "$TAX=1417.61500; printf('%0.5f; %s', $TAX, number_format($TAX, 2, '.', ','));"
    1417.61500; 1,417.62
    I can't replicate this number. What version of PHP are you using?

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Code:
    >php -r "$TAX=1417.61500; printf('%0.5f; %s', $TAX, number_format($TAX, 2, '.', ','));"
    1417.61500; 1,417.62
    I can't replicate this number. What version of PHP are you using?
    php 5.5

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    5.5?
    You'll need to report this as a bug then since 5.5 wouldn't exist beyond a testing stage (if at all, I can't find a snaps for it). I believe to date the highest versions available in the 5 family are 5.4.1RC2 and 5.3.11RC2.


  •  

    Posting Permissions

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