SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually you are using the wrong function to do the rounding.

    There is a function for rounding: round();

    PHP Code:
    echo round(1417.615,2); 

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried it also but still same output

    Thank you

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's strange. For me it is working correct. Maybe some setting in your PHP is not correct.

  5. #5
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where in my settings?



    Thank you

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have read soms more about it and it doesn't appear to be a settings problem. Actually it has to do with binary values.
    but I can't explain that.

    However I might have come across a solution.

    you can update your PHP version or you can give a third parameter to the round function.
    Like: round(1.345, 2, ROUND_HALF_UP).
    I might have written the parameter wrong, but if you try it and get errror, look on google with the parameter I gave you. Good luck

  7. #7
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this:

    Code:
    if($W4_STATUS == 2 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
       $TAX = ($TotEarn - 7917);
       $TAX = (937.50 + ($TAX * .25));
      //  printf('%0.5f; %s', $TAX, number_format($TAX, 2, '.', ','));    
       //$TAX = number_format($TAX, 2, '.', '');
         //$TAX = round($TAX, 2);
        $TAX = round($TAX, 2); 
          $TAX_Ded = number_format($TAX, 2, '.', '');
                                                   
    }
    and it works in my localhost but in server not

  8. #8
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just to satisfy my curiosity, could you run this code and let us know what you got?

    <?php
    $TAX = 417.61552;
    printf('%0.f - %0.2f - %s', $TAX, $TAX, number_format($TAX, 2, '.', ','));
    ?>


    Quote Originally Posted by RvanD85 View Post
    ...
    you can update your PHP version or you can give a third parameter to the round function.
    Like: round(1.345, 2, ROUND_HALF_UP)
    ...
    This parameter needs version 5.3 and it should be PHP_ROUND_HALF_UP

  9. #9
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't change my php version..
    Is there any solution

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    The Netherlands
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhm perhaps write your own rounding function? Can you do that?

  11. #11
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to recheck your code if the php function round(number, number of float digits to be rounded); is not working fine with your code.
    If you still facing problem to round it off just you can go for Javascript function roundNumber(rnum, rlength)

  13. #13
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perhaps you missed seeing my request. Could you run the code below and let us know what you got?

    <?php
    $TAX = 417.61552;
    echo round($TAX, 2).'<br/>';
    printf('%0.f - %0.2f - %s', $TAX, $TAX, number_format($TAX, 2, '.', ','));
    ?>

  14. #14
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I tried your code the output is :

    417.61552-417.62-417.62

    but when I tried:

    PHP Code:
    if($W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
       
    $TAX = ($TotEarn 7917);
       
    $TAX = (937.50 + ($TAX .25));
      
    // $TAX = round($TAX, 2);
       
    $TAX round($TAX2);
       
    //$TAX_Ded = number_format($TAX, 2, '.', '');
         
    $TAX_Ded=printf('%0.f - %0.2f - %s'$TAX$TAXnumber_format($TAX2'.'','));


    the output is:

    1417.610000 - 1417.61 - 1,417.61

    Thank you

  15. #15
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So it is working as it should.

    Just to see that your code works too by changing the last line to this:

    printf('%0.f - %0.2f - %s', $TotEarn, $TAX, number_format($TAX, 2, '.', ','));

    Lets us know what you got.

  16. #16
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this:

    Code:
    if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
       $TAX = ($TotEarn - 7917);
       $TAX = (937.50 + ($TAX * .25));
       
       $TAX = round($TAX, 2);
       //$TAX_Ded = number_format($TAX, 2, '.', '');
       
       
    $TAX_Ded = printf('%0.f - %0.2f - %s', $TotEarn, $TAX, number_format($TAX, 2, '.', ','));
       //$TAX_Ded=printf('%0.f - %0.2f - %s', $TAX, $TAX, number_format($TAX, 2, '.', ','));
    }
    the output is:

    9837.460000 - 1417.61 - 1,417.61

    Thank you

  17. #17
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  18. #18
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, I see your problem. Since your version of PHP does not support ROUND_HALF_UP I would suggest you to do this:

    change this line
    $TAX = (937.50 + ($TAX * .25));

    to this:
    $TAX = (937.500001 + ($TAX * .25));

  19. #19
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tom8 View Post
    OK, I see your problem. Since your version of PHP does not support ROUND_HALF_UP I would suggest you to do this:

    change this line
    $TAX = (937.50 + ($TAX * .25));

    to this:
    $TAX = (937.500001 + ($TAX * .25));
    I tried this and it works,but my problem now is what should I need to my other condition here is my whole code for TAX deduction:

    PHP Code:
    if($W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 2083){
      
    $TAX = (0.00);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 2083){
      
    $TAX = (0.00);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 2083 AND $TotEarn <= 2500) {
      
    $TAX = ($TotEarn 2083);
      
    $TAX = ($TAX .05);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');

    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 2083 AND $TotEarn <= 2500) {
      
    $TAX = ($TotEarn 2083);
      
    $TAX = ($TAX .05);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 2500 AND $TotEarn <= 3333) {
      
    $TAX = ($TotEarn 2500);
      
    $TAX = (20.83 + ($TAX .10));
     
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 2500 AND $TotEarn <= 3333) {
      
    $TAX = ($TotEarn 2500);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }  
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 3333 AND $TotEarn <= 5000) {
      
    $TAX = ($TotEarn 3333);
      
    $TAX =(104.17 + ($TAX .15));
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 3333 AND $TotEarn <= 5000) {
      
    $TAX = ($TotEarn 3333);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5000 AND $TotEarn <= 7917) {
       
    $TAX = ($TotEarn 5000);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5000 AND $TotEarn <= 7917) {
       
    $TAX = ($TotEarn 5000);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
       
    $TAX = ($TotEarn 7917);
       
    //$TAX = (937.50 + ($TAX * .25));
       
    $TAX = (937.500001 + ($TAX .25)); 
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
       
       
    //$TAX_Ded = printf('%0.f - %0.2f - %s', $TotEarn, $TAX, number_format($TAX, 2, '.', ','));
       //$TAX_Ded=printf('%0.f - %0.2f - %s', $TAX, $TAX, number_format($TAX, 2, '.', ','));
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7917 AND $TotEarn <= 12500) {
       
    $TAX = ($TotEarn 7917);
       
       
    $TAX = (937.50 + ($TAX .25));
      
    // $TAX = round($TAX, 2);
       //printf('%0.5f; %s', $TAX, number_format($TAX, 2, '.', ','));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
         
    //$TAX_Ded=printf('%0.f - %0.2f - %s', $TAX, $TAX, number_format($TAX, 2, '.', ','));
        
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 12500 AND $TotEarn <= 22917) {
       
    $TAX = ($TotEarn 12500);
       
    $TAX = (2083.33 + ($TAX .30));
       
    //printf('%0.5f; %s', $TAX, number_format($TAX, 2, '.', ','));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
       
       
    //$TAX_Ded=printf('%0.f - %0.2f - %s', $TAX, $TAX, number_format($TAX, 2, '.', ','));
        
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 12500 AND $TotEarn <= 22917) {
       
    $TAX = ($TotEarn 12500);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 22917 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 22917 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 3125) {
       
    $TAX = (0.00);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 3125) {
       
    $TAX = (0.00);
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 3125 AND $TotEarn <= 3542) {
       
    $TAX = ($TotEarn 3125);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 3125 AND $TotEarn <= 3542) {
       
    $TAX = ($TotEarn 3125);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }  
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 3542 AND $TotEarn <= 4375) {
      
    $TAX = ($TotEarn 3542);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 3542 AND $TotEarn <= 4375) {
      
    $TAX = ($TotEarn 3542);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 4375 AND $TotEarn <= 6042) {
       
    $TAX = ($TotEarn 4375);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 4375 AND $TotEarn <= 6042) {
       
    $TAX = ($TotEarn 4375);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6042 AND $TotEarn <= 8958) {
       
    $TAX = ($TotEarn 6042);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6042 AND $TotEarn <= 8958) {
       
    $TAX = ($TotEarn 6042);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 8958 AND $TotEarn <= 13542) {
       
    $TAX = ($TotEarn 8958);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 8958 AND $TotEarn <= 13542) {
       
    $TAX = ($TotEarn 8958);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 13542 AND $TotEarn <= 23958) {
       
    $TAX = ($TotEarn 13542);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 13542 AND $TotEarn <= 23958) {
       
    $TAX = ($TotEarn 13542);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 23958 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 23958 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 4167) {
       
    $TAX = (0.00);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }   
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 4167) {
       
    $TAX = (0.00);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }   
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 4167 AND $TotEarn <= 4583) {
       
    $TAX = ($TotEarn 4167);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 4167 AND $TotEarn <= 4583) {
       
    $TAX = ($TotEarn 4167);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }  
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 4583 AND $TotEarn <= 5417) {
      
    $TAX = ($TotEarn 4583);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 4583 AND $TotEarn <= 5417) {
      
    $TAX = ($TotEarn 4583);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5417 AND $TotEarn <= 7083) {
       
    $TAX = ($TotEarn 5417);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5417 AND $TotEarn <= 7083) {
       
    $TAX = ($TotEarn 5417);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7083 AND $TotEarn <= 10000) {
       
    $TAX = ($TotEarn 7083);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
       
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7083 AND $TotEarn <= 10000) {
       
    $TAX = ($TotEarn 7083);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 10000 AND $TotEarn <= 14583) {
       
    $TAX = ($TotEarn 10000);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 10000 AND $TotEarn <= 14583) {
       
    $TAX = ($TotEarn 10000);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 14583 AND $TotEarn <= 25000) {
       
    $TAX = ($TotEarn 14583);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 14583 AND $TotEarn <= 25000) {
       
    $TAX = ($TotEarn 14583);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 25000 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 25000 AND $TotEarn <= 50000) {
        
    $TAX = (5208.33);
        
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 5208) {
       
    $TAX = (0.00);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 5208) {
       
    $TAX = (0.00);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5208 AND $TotEarn <= 5625) {
       
    $TAX = ($TotEarn 5208);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }  
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5208 AND $TotEarn <= 5625) {
       
    $TAX = ($TotEarn 5208);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5625 AND $TotEarn <= 6458) {
      
    $TAX = ($TotEarn 5625);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 5625 AND $TotEarn <= 6458) {
      
    $TAX = ($TotEarn 5625);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6458 AND $TotEarn <= 8125) {
       
    $TAX = ($TotEarn 6458);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6458 AND $TotEarn <= 8125) {
       
    $TAX = ($TotEarn 6458);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 8125 AND $TotEarn <= 11042) {
       
    $TAX = ($TotEarn 8125);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 8125 AND $TotEarn <= 11042) {
       
    $TAX = ($TotEarn 8125);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 11042 AND $TotEarn <= 15625) {
       
    $TAX = ($TotEarn 11042);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 11042 AND $TotEarn <= 15625) {
       
    $TAX = ($TotEarn 11042);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 15625 AND $TotEarn <= 26042) {
       
    $TAX = ($TotEarn 15625);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 15625 AND $TotEarn <= 26042) {
       
    $TAX = ($TotEarn 15625);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 26042 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 26042 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 6250) {
       
    $TAX = (0.00);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= AND $TotEarn <= 6250) {
       
    $TAX = (0.00);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6250 AND $TotEarn <= 6667) {
       
    $TAX = ($TotEarn 6250);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }  
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6250 AND $TotEarn <= 6667) {
       
    $TAX = ($TotEarn 6250);
       
    $TAX = ($TAX .05);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');

    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6667 AND $TotEarn <= 7500) {
      
    $TAX = ($TotEarn 6667);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 6667 AND $TotEarn <= 7500) {
      
    $TAX = ($TotEarn 6667);
      
    $TAX = (20.83 + ($TAX .10));
      
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7500 AND $TotEarn <= 9167) {
       
    $TAX = ($TotEarn 7500);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 7500 AND $TotEarn <= 9167) {
       
    $TAX = ($TotEarn 7500);
       
    $TAX = (104.17 + ($TAX .15));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 9167 AND $TotEarn <= 12083) {
       
    $TAX = ($TotEarn 9167);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 9167 AND $TotEarn <= 12083) {
       
    $TAX = ($TotEarn 9167);
       
    $TAX = (354.17 + ($TAX .20));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 12083 AND $TotEarn <= 16667) {
       
    $TAX = ($TotEarn 12083);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 12083 AND $TotEarn <= 16667) {
       
    $TAX = ($TotEarn 12083);
       
    $TAX = (937.50 + ($TAX .25));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 16667 AND $TotEarn <= 27083) {
       
    $TAX = ($TotEarn 16667);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 16667 AND $TotEarn <= 27083) {
       
    $TAX = ($TotEarn 16667);
       
    $TAX = (2083.33 + ($TAX .30));
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 27083 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 27083 AND $TotEarn <= 50000) {
        
    $TAX = (5208.33); 
       
    $TAX round($TAX2);
        
    $TAX_Ded number_format($TAX2'.''');
    }
    else{
        
    $TAX_Ded round(0.00);


    Thank you so much

  20. #20
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First set your TAX to 0.00 and then remove all the round() and $TAX_Ded. Do your adjustment and rounding after all the tax calculations.

    Since you donít add taxes till your TotEarn is greater than 2083 you would start at that point:

    PHP Code:
    $TAX 0.00;

    if(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 2083 AND $TotEarn <= 2500) {
      
    $TAX = ($TotEarn 2083);
      
    $TAX = ($TAX .05);
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 2083 AND $TotEarn <= 2500) {
      
    $TAX = ($TotEarn 2083);
      
    $TAX = ($TAX .05);
    }

       ...
               
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 16667 AND $TotEarn <= 27083) {
       
    $TAX = ($TotEarn 16667);
       
    $TAX = (2083.33 + ($TAX .30));
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 27083 AND $TotEarn <= 50000) {
       
    $TAX = (5208.33);
    }
    elseif(
    $W4_STATUS == AND $DEPENDENTS == AND $TotEarn >= 27083 AND $TotEarn <= 50000) {
        
    $TAX = (5208.33); 
    }
    else{
    }  

    $TAX .= 0.00001;
    $TAX round($TAX2);
    $TAX_Ded number_format($TAX2'.'''); 

  21. #21
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I will try this.


    Thank you so much

  22. #22
    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)
    Flat round it.

    Round(<number> * 100) / 100

    So if the number is 216.155:
    Number * 100 = 21615.5
    Flat round: 21616
    Divide: 216.16
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  23. #23
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Instead of using comparison statements (if and elseif) you could also use database, which I believe will make it easier to maintain.

    Here’s how:

    First get all your calculation reference values
    (W4_STATUS, DEPENDENTS, more than TotEarn, less than TotEarn, deduction, tax rate)
    and put them in a plain text file

    I have done that so all you need to do is to verify the values -

    tax.txt:
    Code:
    1 0 0 2083 0 0.00
    1 0 2083 2500 2083 0.00 .05
    1 0 2500 3333 2500 20.83 .10
    1 0 3333 5000 3333 104.17 .15
    1 0 5000 7917 5000 354.17 .20
    1 0 7917 12500 7917 937.50 .25
    1 0 12500 22917 12500 2083.33 .30
    1 0 22917 50000 0 5208.33
    1 1 0 3125 0 0.00
    1 1 3125 3542 3125 0.00 .05
    1 1 3542 4375 3542 20.83 .10
    1 1 4375 6042 4375 104.17 .15
    1 1 6042 8958 6042 354.17 .20
    1 1 8958 13542 8958 937.50 .25
    1 1 13542 23958 13542 2083.33 .30
    1 1 23958 50000 0 5208.33
    1 2 0 4167 0 0.00
    1 2 4167 4583 4167 0.00 .05
    1 2 4583 5417 4583 20.83 .10
    1 2 5417 7083 5417 104.17 .15
    1 2 7083 10000 7083 354.17 .20
    1 2 10000 14583 10000 937.50 .25
    1 2 14583 25000 14583 2083.33 .30
    1 2 25000 50000 0 5208.33
    1 3 0 5208 0 0.00
    1 3 5208 5625 5208 0.00 .05
    1 3 5625 6458 5625 20.83 .10
    1 3 6458 8125 6458 104.17 .15
    1 3 8125 11042 8125 354.17 .20
    1 3 11042 15625 11042 937.50 .25
    1 3 15625 26042 15625 2083.33 .30
    1 3 26042 50000 0 5208.33
    1 4 0 6250 0 0.00
    1 4 6250 6667 6250 0.00 .05
    1 4 6667 7500 6667 20.83 .10
    1 4 7500 9167 7500 104.17 .15
    1 4 9167 12083 9167 354.17 .20
    1 4 12083 16667 12083 937.50 .25
    1 4 16667 27083 16667 2083.33 .30
    1 4 27083 50000 0 5208.33
    2 0 2083 2500 2083 0 .05
    2 0 2500 3333 2500 20.83 .10
    2 0 3333 5000 3333 104.17 .15
    2 0 5000 7917 5000 354.17 .20
    2 0 7917 12500 7917 937.50 .25
    2 0 12500 22917 12500 2083.33 .30
    2 0 22917 50000 0 5208.33
    2 1 0 3125 0 0.00
    2 1 3125 3542 3125 0.00 .05
    2 1 3542 4375 3542 20.83 .10
    2 1 4375 6042 4375 104.17 .15
    2 1 6042 8958 6042 354.17 .20
    2 1 8958 13542 8958 937.50 .25
    2 1 13542 23958 13542 2083.33 .30
    2 1 23958 50000 0 5208.33
    2 2 0 4167 0 0.00
    2 2 4167 4583 4167 0.00 .05
    2 2 4583 5417 4583 20.83 .10
    2 2 5417 7083 5417 104.17 .15
    2 2 7083 10000 7083 354.17 .20
    2 2 10000 14583 10000 937.50 .25
    2 2 14583 25000 14583 2083.33 .30
    2 2 25000 50000 0 5208.33
    2 3 0 5208 0 0.00
    2 3 5208 5625 5208 0.00 .05
    2 3 5625 6458 5625 20.83 .10
    2 3 6458 8125 6458 104.17 .15
    2 3 8125 11042 8125 354.17 .20
    2 3 11042 15625 11042 937.50 .25
    2 3 15625 26042 15625 2083.33 .30
    2 3 26042 50000 0 5208.33
    2 4 0 6250 0 0.00
    2 4 6250 6667 6250 0.00 .05
    2 4 6667 7500 6667 20.83 .10
    2 4 7500 9167 7500 104.17 .15
    2 4 9167 12083 9167 354.17 .20
    2 4 12083 16667 12083 937.50 .25
    2 4 16667 27083 16667 2083.33 .30
    2 4 27083 50000 0 5208.33
    Next create a database. Use sqlite if your PHP is version 5; if not then use MySQL or any database that is available to you.

    Here is a simple sqlite script to create a database and insert those values above.

    create_table.php
    PHP Code:
    <?php
       $table 
    = new SQLiteDatabase("tax.sqlite");
       
    $table->query("CREATE TABLE tax".
             
    " ( txid INTEGER PRIMARY KEY,Status INTEGER, Dependents INTEGER,More INTEGER,Less INTEGER,Deduction CHAR,Tax Char,Rate CHAR )");
       
    $data explode("\n",@file_get_contents("tax.txt"));
       
    $num     count($data);
       for(
    $ix=0;$ix<$num;$ix++)
       {
           
    $items explode(" ",$data[$ix]);
           if(!
    $items[0]) continue;
           
    $ins++;
           
    $table->query("INSERT INTO tax VALUES(NULL,'$items[0]','$items[1]','$items[2]','$items[3]','$items[4]','$items[5]','$items[6]')");
       }
       echo 
    "Tax table created and $ins records added";
    ?>
    Now the tax calculation values are ready to be used for your function.

    Here is an example of the script (tax.php):
    PHP Code:
    <?php
       $status 
    $_POST['W4_STATUS'];
       
    $dependents $_POST['DEPENDENTS'];
       
    $earning $_POST['TotEarn'];
       echo 
    '
    <form method="post" action="tax.php">
       W4_STATUS: <input name="W4_STATUS" value="'
    .$status.'"/><br/><br/>
       DEPENDENTS: <input name="DEPENDENTS" value="'
    .$dependents.'"/><br/><br/>
       TotEarning: <input name="TotEarn" value="'
    .$earning.'"/><br/><br/>
       <input type="submit" name="tax" value="Compute"/>
    </form>
    '
    ;
       if(
    $_POST['tax'])
       {
           
    $tax = new SQLiteDatabase("tax.sqlite");
           
    $q $tax->query("SELECT Deduction,Tax,Rate FROM tax WHERE Status='$status' and Dependents='$dependents' and More < '$earning' and Less > '$earning'");
           if(
    $q)
           {
               list(
    $deduction,$tax,$rate) = $q->fetch();
               
    $taxable 0;
               if(
    $deduction$taxable = ($earning $deduction) * $rate;
               
    $tax += $taxable+.0001;  // adjust for rounding up
               
    echo "<h3>Your amount tax due is $".round($tax,2)."</h3>";
           }
           else echo 
    "Database Error...";
       }
    ?>
    Click on the link below to test the above script:
    http://w-cms.org/sqlite/tax.php

  24. #24
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this post #20 solves my problem.

    Thank you


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
  •