|
Calc Builder SupportCalc Builder support |
| Public write access is disabled. Please login |
![]() | Last post |
![]() jennik 4 posts --- |
29.12.2011 13:29Rounding numbers up and downHallo everyone. I have just encountered an interesting problem in my calculations... I need to round up numbers sometimes normally (1.1 -> 1; 1.5 -> 2) and sometimes I need to force rounding down (1.9 -> 1) sometimes up (1.1 -> 2). Why? Because when the result of 2.9 business cards to be printed together on one A4 paper means that there will only be 2 of them usable. While unable of any advanced PHP coding I have seemingly solved it via this formula: // Let's have the input number $num rounded normally $num_round=number_format($num,0); // Let's substract the rounded value from the input one $subst=$num-$num_round; // Let's force $num to be rounded up everytime // If the result is biger then 0, add 1 to the rounded number if ($subst > 0) $num_round_up=$num_round+1; else $num_round_up=$num_round; // Let's force $num to be rounded down everytime // if the result is bellow 0, substract 1 from initial rounded number if ($subst < 0) $num_round_down=$num_round-1; else $num_round_down=$num_round; This works quite well with any bumber up to 999... but, above thousand it's messed up thanks to the fact, that the initial rounded number features "," divider - 1,000 and it's broking any other math processes. Please, could you recommend any other reliable solution for rounding the resulting number up and down while letting it be pur number in the end? You can download my calculator here: http://files.prazak.net/calcbuilder_rounding_numbers.zip |
![]() jennik 4 posts --- |
29.12.2011 13:42 Re: Rounding numbers up and down Just solved it $num_round=number_format($num,0); $num_round=str_replace( ',' , '' , $num_round); $subst=$num-$num_round; if ($subst > 0) $num_round_up=$num_round+1; else $num_round_up=$num_round; if ($subst < 0) $num_round_down=$num_round-1; else $num_round_down=$num_round; |
![]() MSTeam 227 posts --- |
29.12.2011 13:45 Re: Rounding numbers up and down Hello,although you can use number_format function to get some number rounding, for your case is not the best option. We suggest this way: http://php.net/manual/en/function.round.php this function will help you to apply round with different methods (up,down,half_even and down...). You should use this function to make all number handling, when you are operating with numbers. When you finish all calculations, then you can use number_format to get your output with decimal and thousand characters as needed. Hope this helps, best regards Moonsoft Team OK, you found another workaround as we were just writting...we'll done! Edited by MSTeam - 29.12.2011 14:46 Moonsoft Team www.moonsoft.es |
![]() jennik 4 posts --- |
29.12.2011 14:16 Re: Rounding numbers up and down Thanks for help! I tried the page before and wasn't able to solve the thing either way.I tried the floor() and ceil() functions, didn't help. But, round() is good for my function to spare str_replace() line: $num_round=round($num,0); $subst=$num-$num_round; if ($subst > 0) $num_round_up=$num_round+1; else $num_round_up=$num_round; if ($subst < 0) $num_round_down=$num_round-1; else $num_round_down=$num_round; |
Pre-sales questions/Custom requests: gestion@moonsoft.es
©2012 Moonsoft Software Solutions | Designed and developed by Moonsoft