Page 1 of 1
Round a calculation result.
Posted: Mon Aug 22, 2011 5:13 am
by wizzuriz
Hello all.
I have a tricky one.
I want to round my calculation result with 2.5
Example : 139.90 should become 140.00
Example : 136.01 should become 135.00
Example : 137.11 should become 137.50
In other words is hold round to the nearest in the 2.5 range. 2.5 - 5.00 - 7.50 - 10.00 - 12.50 - 15.00 132.50 - 135.00 - 137.50 - 140.00 any idea how to do that.
Best regards
Wizzuriz
PS. if you need to see more please let me know.
Re: Round a calculation result.
Posted: Mon Aug 22, 2011 10:53 am
by jacek
You can use round() to round a number.
echo round(139.90); // 140
but to round to the nearest .5 you will need to use some more maths.
echo round(139.9 * 2) / 2;
maybe ?
Re: Round a calculation result.
Posted: Mon Aug 22, 2011 2:39 pm
by wizzuriz
Hey Jacek.
Thanks for the fast replay, the thing is that I don't just need it to round it I need it to select the correct value.
its the result is 40.39 it should be 40.00 but if its 41.39 it should be 42.50.
<td id="tdgraa"><P>Step 0,2 <?php printf("%5.2f\n",$Display_step2);?></p></td> don´t round any but it will let me display the .00 and if I use <?php echo round($Display_step1 *2) /2;?> it rounds it to 41.50 and its should be 42.50 or 40.00 one or the other.
any idea?
Best regards
wizzuriz.
Re: Round a calculation result.
Posted: Tue Aug 23, 2011 11:18 am
by jacek
I don't really get how your rounding works. why should 41.39 wound up to 42.5 and not just 42 ?
Re: Round a calculation result.
Posted: Tue Aug 23, 2011 1:06 pm
by Carbine
jacek wrote:I don't really get how your rounding works. why should 41.39 wound up to 42.5 and not just 42 ?
He wants to round to the nearest multiple of 2.5.
Re: Round a calculation result.
Posted: Tue Aug 23, 2011 6:51 pm
by wizzuriz
Hello all.
I'm new in programming and I have a problem with rounding some values.
how could this work?
function round($my_number){
I don´t know how to put in xx value so i use * please let me know if there is a other way because the value cloud be 40.59 or 81.31 that i need to round in the same function.
If ($my_number =<*1.25);
round floor(($my_number)/2.5)*2.5;
}
else if round celi(($my_number =>*1.26)/2.5*2.5;
}
My results will be 40.00 to 200.00
The thing is what ever value I get from my calculation 40.00 , 42.50 , 45.00 , 47.50 or 50.00
My problem is if I use ceil(intval($my_number)/2.5)*2.5; is that if the result is 40.01 it will still round up.
Please let me know if you have a function, and if you cloud please show the echo round_function($my_number); så I know how to echo it on my page.
Thanks all.
Best regards
wizzuriz
Re: Round a calculation result.
Posted: Tue Aug 23, 2011 8:14 pm
by jacek
Carbine wrote:jacek wrote:I don't really get how your rounding works. why should 41.39 wound up to 42.5 and not just 42 ?
He wants to round to the nearest multiple of 2.5.
Well would
echo round($number * 4, 1) / 4;
do that ?
Re: Round a calculation result.
Posted: Tue Aug 23, 2011 8:40 pm
by wizzuriz
Thank for the help but it still is not the right value.
I get 48,75 its should be 47,50 or 50.00 not 47,75
round ($number* 4, 1) / 4
That don't work.
best regards
wizzuriz
Re: Round a calculation result.
Posted: Wed Aug 24, 2011 1:17 am
by jacek
Maybe replace 4 with 8
this can work, just meed to get the number right.
For some reason I have only looked at this topic at stupid times so can't be bothered to get paper to work it out.
Re: Round a calculation result.
Posted: Wed Aug 24, 2011 1:48 pm
by wizzuriz
Hi Jacek.
Can you please tell me the idea of the calculation, what is the different from 4,1/4 and 8,1 /8 ? it give the same still 48,75.
If I can understand the idea on how to make it I can calculate it myself.
A other ting is do you think you can help me build a function that can make my calculation more simple?
I think a while function cloud work with this.
My calculation is 90 different values but its a same formula and now i just have 90 lines with the same calculation where I just change the $specific_value for each calculation.
see this is my calculation
[syntax=php]
$result_x1 = $bcl109*pow($bcp_aa15_op1,$op2)*$value*$nk3*$oul87;
[/syntax]
I don't understand the concept on how to create / build a function yet but is reading on that part.
I'm working on something like this.
// this function should get $value from the database and return it as $value then it should take all the other values and return them and each time pass them to the calculation and return them all in a array where I can take them out.
[syntax=php]$result=array();
function calculation_multiple_times(){
$value = SELECT
"`value` FROM `calculations_values`
WHERE `user_name` = '{$user}'
mysql_query=$value;
Return =$value;
while = value = 1 : 0
$result = $bcl109*pow($bcp_aa15_op1,[b]$value[/b])*$uv1*$nk3*$oul87;
}[/syntax]
Now this don't work but I think there would be a way to calculate all values in the same function ??
Please let me know if you know how to build such a function or know where I can get information about it.
Best regards
Wizzuirz.
Re: Round a calculation result.
Posted: Wed Aug 24, 2011 6:06 pm
by jacek
wizzuriz wrote:Can you please tell me the idea of the calculation, what is the different from 4,1/4 and 8,1 /8 ? it give the same still 48,75.
If you use 2 it rounds to the nearest 0.5
Take the example of 10.3
10.3 * 2 = 20.6
round(20.6) = 21
21 / 2 = 10.5
If we use 4 instead of 2
10.3 * 4 = 41.2
round(41.2) = 41
41 / 4 = 10.25
rounding to the nearest 0.25
So you want wounding to the nearest 2.5, so if we use 0.4
41.39 * 0.4 = 16.556
round(16.556) = 17
17 / 0.4 = 42.5
The formula to work out the number you multiply by is
1 / [what you want to round to] = number
In this case
1 / 2.5 = 0.4