newphpcoder Posted April 18, 2012 Share Posted April 18, 2012 Hi.. I encountered problem in rounding of numbers into two decimal places. here is my sample code: if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) { $TAX = ($TotEarn - 7917); $TAX = (937.50 + ($TAX * .25)); $TAX = number_format($TAX, 2, '.', ''); } for example from this: $TAX = ($TotEarn - 7917); $TAX = (937.50 + ($TAX * .25)); the output is: 1417.615 using this: $TAX = number_format($TAX, 2, '.', ''); the output was : 1417.61 but it should be : 1417.62 Thank you Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/ Share on other sites More sharing options...
Philip Posted April 18, 2012 Share Posted April 18, 2012 You're looking for round not number_format Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338307 Share on other sites More sharing options...
newphpcoder Posted April 18, 2012 Author Share Posted April 18, 2012 I tried round and yet still same output Thank you Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338317 Share on other sites More sharing options...
Philip Posted April 18, 2012 Share Posted April 18, 2012 http://codepad.viper-7.com/YB7QR9 shows otherwise Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338324 Share on other sites More sharing options...
MMDE Posted April 18, 2012 Share Posted April 18, 2012 While you should use number_format, round would be useful even if it only round off to the nearest whole number because you can do this: round(100*$number)/100 Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338328 Share on other sites More sharing options...
requinix Posted April 18, 2012 Share Posted April 18, 2012 While you should use number_format, round would be useful even if it only round off to the nearest whole number because you can do this: round(100*$number)/100 Or the more direct round($number, 2) Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338330 Share on other sites More sharing options...
newphpcoder Posted April 18, 2012 Author Share Posted April 18, 2012 I tried it also, but still same output. Thank you Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338332 Share on other sites More sharing options...
premiso Posted April 18, 2012 Share Posted April 18, 2012 removed, never mind What is your original value for $TotEarn ? Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338434 Share on other sites More sharing options...
newphpcoder Posted April 19, 2012 Author Share Posted April 19, 2012 if($W4_STATUS == 1 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 = (9837.46 - 7917); $TAX = (937.50 + (1920.46 * .25)); // (937.50 + 480.115 ) $TAX = number_format(1417.615, 2) Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338630 Share on other sites More sharing options...
litebearer Posted April 19, 2012 Share Posted April 19, 2012 Aside... It would be better (IMHO) if you did not use the same variable for different 'meanings'. ie $excess_taxable_income vs $tax; keeping $tax as being the tax dollars. Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1338638 Share on other sites More sharing options...
newphpcoder Posted April 23, 2012 Author Share Posted April 23, 2012 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 Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1339722 Share on other sites More sharing options...
litebearer Posted April 23, 2012 Share Posted April 23, 2012 try... $TAX = round($TotEarn - 7917, 2); $TAX = round($TAX * .25, 2); echo $TAX; Link to comment https://forums.phpfreaks.com/topic/261151-round-of-into-two-decimal-places/#findComment-1339732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.