Jump to content

How to round up?


solarisuser

Recommended Posts

Hello,

I am writing code for CPU Speed detection, and then inserts it into a MySQL DB.  I have finished this part.

What I need help with:

I need to look at my speed variable, which will be something like 500, 800, or 3000.
For example, if the CPUspeed variable is set to 1700, I want it to be modified to 1800.  If its 1800, then leave it alone (or modify to 1800).  If its 1690, I want it to be 1600.
If its 3000, I want it to be 3000.  If its 3110, I want it to be 3200.

I will do the Mhz and Ghz distinction later.

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/
Share on other sites

an example only
[code]

<?php

$speed1="199";
$speed2="299";
$speed3="2999";
$speed4="3199";

if($speed1 < 200 >200){

$speed1="200";

}elseif($speed2 < 299 > 299){

$speed2="300";

}elseif($speed3 < 2999 > 2999){

$speed3="3000";

}elseif($speed > 3199 < 3199 )

$speed4="3200";

}

echo "<br> $speed1 <br> $speed2 <br> $speed3 <br> $speed4 <br> ";

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83720
Share on other sites

sorry but i coudnt get this to go i fill well gutted man

[code]
<?php

$speed1="199";
$speed2="299";
$speed3="2999";
$speed4="3199";

if(($speed1 < 200)||($speed1 > 200)){


$speed1="200";


}elseif(($speed2 > 300 )||($speed2 < 300)){

$speed2="300";


}elseif(($speed3 > 3000)||($speed3 < 3000)){

$speed3="3000";

}elseif(($speed4 > 3200 )||($speed4 < 3200)){

$speed4="3200";

}

echo "<br> $speed1 <br> $speed2 <br> $speed3 <br> $speed4 <br> ";

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83745
Share on other sites

Simpler rounding algorithm

[code]<?php
$num = 1740; // example
$toNearest = 500;

$newnum = round($num/$toNearest) * $toNearest;

echo $num. " -> ". $newnum;
?>[/code]

If you always want to round down, replace round() with floor()

If you always want to round up, replace round() with ceil()
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83746
Share on other sites

[quote author=solarisuser link=topic=106423.msg425612#msg425612 date=1157062062]
Hello,

I am writing code for CPU Speed detection, and then inserts it into a MySQL DB.  I have finished this part.

What I need help with:

I need to look at my speed variable, which will be something like 500, 800, or 3000.
For example, if the CPUspeed variable is set to 1700, I want it to be modified to 1800.  If its 1800, then leave it alone (or modify to 1800).  If its 1690, I want it to be 1600.
If its 3000, I want it to be 3000.  If its 3110, I want it to be 3200.

I will do the Mhz and Ghz distinction later.

Thanks!
[/quote]

This is such a weird rounding. If you were rounding to the 10th place, 1750 = 1800. 1690 would be 1700...3110 would be 3100....but your specifications are different.
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83751
Share on other sites

The spec appears to be "Round to nearest 200"

[code]<?php
$nums = array(1690,1700,1800, 3000, 3110); // example
$toNearest = 200;

foreach ($nums as $num) {
    $newnum = round($num/$toNearest) * $toNearest;
    echo $num. " -> ". $newnum . '<br>';
}
?>[/code]

gives--->
[pre]
1690 -> 1600
1700 -> 1800
1800 -> 1800
3000 -> 3000
3110 -> 3200

[/pre]
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83757
Share on other sites

yes Jenk, we know of the existence of ceil and floor. but look at the numbers vs. how he wants them rounded.  can you give us an example code that will satisfy the conditions of those examples, using only ceil and floor? I think Barand has it right: round to the nearest 200.  if solariuser would chime in and confirm that...
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83970
Share on other sites

so if the user has the number 1310 and he needs to select based on 1400, at what point does your solution figure out $x and $y?  how would go about doing that? if he has 1310 and he needs to select * from table where 'speed' = 1400, your query doesn't make sense to me at all. perhaps you can explain it more? After all, I am not the brightest crayon in the box...
Link to comment
https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-84135
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.