Jump to content


Photo

How to round up?


  • Please log in to reply
16 replies to this topic

#1 solarisuser

solarisuser
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 31 August 2006 - 10:07 PM

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!

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 31 August 2006 - 10:15 PM

http://us3.php.net/round
PHP Ninja

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 31 August 2006 - 10:34 PM

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...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 31 August 2006 - 10:52 PM

an example only

<?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> ";

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 31 August 2006 - 10:59 PM

uhh...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 31 August 2006 - 11:10 PM

<?php
$raw = 1690; // example
$raw2 = $raw/200;
$bit = $raw2 - intval($raw2);
$newnum = 200* (intval($raw2) + ($bit>=0.5));
echo $raw. " -> ". $newnum;
?>

Works for me with various values
Legend has it that reading the manual never killed anyone.
My site

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 31 August 2006 - 11:12 PM

@redarrow

An example of what ?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 31 August 2006 - 11:25 PM

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

<?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> ";

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 31 August 2006 - 11:26 PM

Simpler rounding algorithm

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

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

echo $num. " -> ". $newnum;
?>

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

If you always want to round up, replace round() with ceil()

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 31 August 2006 - 11:28 PM

thank you but that looks relly hard to me another for the white board for learning thanks.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 31 August 2006 - 11:51 PM

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!


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.

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 01 September 2006 - 12:03 AM

The spec appears to be "Round to nearest 200"

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

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

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

[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 08:01 AM

uh.. guys..

<?php

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

?>


#14 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 September 2006 - 11:56 AM

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...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#15 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 11:59 AM

I know Barand's code works with 500 changed to 200. I know 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
Legend has it that reading the manual never killed anyone.
My site

#16 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 01:47 PM

It's all unecessary anyway.. the OP can just
SELECT * FROM `table` WHERE `speed` < $x and `speed` > $y
.. but no one spotted that ;)

#17 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 September 2006 - 05:06 PM

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...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users