www.webdeveloper.com
Results 1 to 5 of 5

Thread: Round of into two decimal places

  1. #1
    Join Date
    Oct 2011
    Posts
    350

    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. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    $TAX=round($TAX,2,PHP_ROUND_HALF_UP);

  3. #3
    Join Date
    Oct 2011
    Posts
    350
    I tried some testing:
    $TAX = ($TotEarn - 7917); // 1920.46
    $TAX = ($TAX * . 25); // 480.115

    $TAX = round($TAX, 2) // 480.11

    but when I tried this:

    $TAX = (480.115);
    $TAX = round($TAX , 2); // 480.12

    I notice that if the variable is = actual numeric it works but if it is came from variable it did not work

    I really don't know what to do

    Thank you

  4. #4
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    $TAX=round($TAX,2,PHP_ROUND_HALF_UP);

    or

    $TAX=round($TAX,2,PHP_ROUND_HALF_DOWN);

    depending on which way you want it to go.

  5. #5
    Join Date
    Oct 2011
    Posts
    350
    I resolve my problem in round off into two decimal places by this code:

    Code:
    $TAX = 0.00; 
    
    if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 2083 AND $TotEarn <= 2500) { 
      $TAX = ($TotEarn - 2083); 
      $TAX = ($TAX * .05); 
    } 
    elseif($W4_STATUS == 2 AND $DEPENDENTS == 0 AND $TotEarn >= 2083 AND $TotEarn <= 2500) { 
      $TAX = ($TotEarn - 2083); 
      $TAX = ($TAX * .05); 
    } 
    
       ... 
                
    elseif($W4_STATUS == 2 AND $DEPENDENTS == 4 AND $TotEarn >= 16667 AND $TotEarn <= 27083) { 
       $TAX = ($TotEarn - 16667); 
       $TAX = (2083.33 + ($TAX * .30)); 
    } 
    elseif($W4_STATUS == 1 AND $DEPENDENTS == 4 AND $TotEarn >= 27083 AND $TotEarn <= 50000) { 
       $TAX = (5208.33); 
    } 
    elseif($W4_STATUS == 2 AND $DEPENDENTS == 4 AND $TotEarn >= 27083 AND $TotEarn <= 50000) { 
        $TAX = (5208.33);  
    } 
    else{ 
    }   
    
    $TAX .= 0.00001; 
    $TAX = round($TAX, 2); 
    $TAX_Ded = number_format($TAX, 2, '.', '');
    Thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles