CalcBuilder Forum

Rounding numbers up and down

User jennik 2011-12-29 13:29:55

Hallo 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

User jennik 2011-12-29 13:42:10

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;

Moonsoft support 2011-12-29 13:45:22

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

User jennik 2011-12-29 14:16:39

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;

...
Support/development 40 hours

With the peace of mind of having a professional team at your service (20% discount)

Buy now!
...
List Manager

Build different lists for your site

Buy now!