#### Archived

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

# How to round up?

## 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!

##### Share on other sites
http://us3.php.net/round

##### Share on other sites
well there doesn't seem to be an apparent pattern to those numbers, as in, round up everything, or round to the nearest whole number sort of thing.  So you may just have to make a custom list to convert things from one thing to another...

##### 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]

uhh...

##### Share on other sites
[code]<?php
\$raw = 1690; // example
\$raw2 = \$raw/200;
\$bit = \$raw2 - intval(\$raw2);
\$newnum = 200* (intval(\$raw2) + (\$bit>=0.5));
echo \$raw. " -> ". \$newnum;
?>[/code]

Works for me with various values

##### Share on other sites
@redarrow

An example of what ?

##### 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]

##### 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()

##### Share on other sites
thank you but that looks relly hard to me another for the white board for learning thanks.

##### Share on other sites
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.

##### 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]

##### Share on other sites
uh.. guys..

[code]<?php

\$val = 1.5;
\$roundedVal = ceil(\$val); // 2
\$roundedVal = floor(\$val); // 1

?>[/code]

##### 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...

##### Share on other sites
I [b]know[/b] Barand's code works with 500 changed to 200. I [b]know[/b] mine works as well.

As CV says, all we need now is the original poster to show up and we can all move on  ;D

##### Share on other sites
It's all unecessary anyway.. the OP can just [code]SELECT * FROM `table` WHERE `speed` < \$x and `speed` > \$y[/code] .. but no one spotted that ;)

##### 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...