solarisuser Posted August 31, 2006 Share Posted August 31, 2006 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 More sharing options...
Caesar Posted August 31, 2006 Share Posted August 31, 2006 http://us3.php.net/round Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83709 Share on other sites More sharing options...
.josh Posted August 31, 2006 Share Posted August 31, 2006 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... Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83717 Share on other sites More sharing options...
redarrow Posted August 31, 2006 Share Posted August 31, 2006 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 More sharing options...
.josh Posted August 31, 2006 Share Posted August 31, 2006 uhh... Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83727 Share on other sites More sharing options...
AndyB Posted August 31, 2006 Share Posted August 31, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83736 Share on other sites More sharing options...
Barand Posted August 31, 2006 Share Posted August 31, 2006 @redarrowAn example of what ? Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83738 Share on other sites More sharing options...
redarrow Posted August 31, 2006 Share Posted August 31, 2006 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 More sharing options...
Barand Posted August 31, 2006 Share Posted August 31, 2006 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 More sharing options...
redarrow Posted August 31, 2006 Share Posted August 31, 2006 thank you but that looks relly hard to me another for the white board for learning thanks. Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83748 Share on other sites More sharing options...
extrovertive Posted August 31, 2006 Share Posted August 31, 2006 [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 More sharing options...
Barand Posted September 1, 2006 Share Posted September 1, 2006 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 -> 16001700 -> 18001800 -> 18003000 -> 30003110 -> 3200[/pre] Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83757 Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 uh.. guys.. [code]<?php$val = 1.5;$roundedVal = ceil($val); // 2$roundedVal = floor($val); // 1?>[/code] Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83886 Share on other sites More sharing options...
.josh Posted September 1, 2006 Share Posted September 1, 2006 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 More sharing options...
AndyB Posted September 1, 2006 Share Posted September 1, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-83972 Share on other sites More sharing options...
Jenk Posted September 1, 2006 Share Posted September 1, 2006 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 ;) Link to comment https://forums.phpfreaks.com/topic/19300-how-to-round-up/#findComment-84026 Share on other sites More sharing options...
.josh Posted September 1, 2006 Share Posted September 1, 2006 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.