PHP - Rounding Down To Nearest 5 Or 0
So I just started working with php (not programming) a couple days ago. In this formula, I need to round something down to the nearest 5 OR 0. Whatever comes first. Any thoughts on how i might do this?
Here's what I have so far. <?php $magicLevel = $_POST['magicLevel']; $playerLevel = $_POST['playerLevel']; $rune = $_POST['rune']; //gets the value "lmm" $maxDamage = 0; switch($rune) { case "lmm": $maxDamage = ($playerLevel*0.2)+($magicLevel*0.81)+4; break; } echo $maxDamage; ?> What I need to round down is this part: ($playerLevel*0.2) Thanks in advance! Similar TutorialsI need to take a time and convert so it is rounded up or down to the nearest 0 or 5. Code: [Select] function roundMe ($time, $upDown) { if($upDown==1) { //round up } else { //round down } return $x; } echo roundMe("16:22", 1); // returns 16:25 echo roundMe("16:22", 0); // returns 16:20 echo roundMe("16:25", 1); // returns 16:25 echo roundMe("16:59", 1); // returns 17:00 echo roundMe("16:59", 0); // returns 16:55 How can I order this query of zip codes within the radius by nearest? /** * Get Zipcode By Radius * @param string $zip US zip code * @param ing $radius Radius in miles * @return array List of nearby zipcodes */ protected function get_zipcodes_by_radius ( $zip, $radius ) { $sql = 'SELECT distinct(zip) FROM zip_codes WHERE (3958*3.1415926*sqrt((latitude-'.$zip->latitude.')*(latitude-'.$zip->latitude.') + cos(latitude/57.29578)*cos('.$zip->latitude.'/57.29578)*(longitude-'.$zip->longitude.')*(longitude-'.$zip->longitude.'))/180) <= '.$radius.';'; $zip_codes = array(); if ( $result = $this->db->query( $sql ) ) { while( $row = $result->fetch_object() ) { array_push( $zip_codes, $row->zip ); } $result->close(); } return $zip_codes; }
hello, if i have the following, how would i display it? Code: [Select] function minutes_round ($hour = "$signintime", $minutes = '5', $format = "H:i") { $seconds = strtotime($hour); $rounded = round($seconds / ($minutes * 60)) * ($minutes * 60); return date($format, $rounded); } How do I find the nearest div of dynamically added html?
hi there all, I need some help for rounding currency issue.. example: 0.98 round to 0.95 0.95 when round = 0.95 0.91 roun to 0.95. mean. i wanna round the end of number 0.98. if the end of number is 0.01,0.02,0.03&0.04 it would be automatic round to nearest 0.05 *0.01 round = 0.05 *if the mineral water =$0.71 i should get $0.75 and if the end of number is 0.06,0.07,0.08,0.09 it would automatic round to nearest -0.05 *0.06 round = 0.05 *if the mineral water = $0.79 then i should get $0.75 i had googling this for 2months and no answer.. please help me.TQ How can I round this with 2 decimals: Code: [Select] echo " $row[team_treasure] "; I've tried round($row[team_treasure],2) but doesn't seem to work inside my echo tags :/ Hi all, Can anyone please help me with my problem. I want to know how to round a decimal - eg. 12.34 to the nearest .10 so it would be 12.30 or if 12.35 go to 12.30 or 12.40. // my example: round(12.34, 2); // I want to to go to: 12.30 Any help is greatly appreciated. I want to round off an integer vaule to 10-6 grd from 52.71666666 to 52716666 0.926888888 to 926888 does anyone know a simple way to do this? I am trying to take a number (three for example) and want to round it to five, or sixty-seven up to seventy. I looked at round and ceil, but those don't seem like they are the right things. Thanks, Joseph Bascially i add a couple numbers up and display the sum. sometimes the number is something like this: 234.237 i want it to display : 234.24 i realize its needs "round()" in it but i dont know how to use it.. any help? Hi, Having trouble rounding the output. I would need to be sure the output is not longer than 2 spaces after comma. Any ideas? My code Code: [Select] <script type="text/javascript"> $(document).ready(function() { $("<?php echo "#".$input_end_time; ?>").blur(function(){ var start_time = $("<?php echo "#".$input_start_time; ?>").val(); var end_time = $("<?php echo "#".$input_end_time; ?>").val(); $.getJSON("include/timedifferencecalculation.php?s="+start_time+"&e="+end_time, function(data){ var success = data.success; if (success == 1) { $("<?php echo "#".$input_difference; ?>" ).val(data.hours + " h " + data.minutes + " m "); if (data.hours + data.minutes/60 >= 6){ $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); }else { $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60); } }else { $("#message").text("Viga! " + data.reason); } }) }); }); </script> Code: [Select] This is before $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); This is what im trying to get but its not working. $("<?php echo "#".$hours_difference; ?>").val( round(data.hours + data.minutes/60 - 0.5), 2 ); Hey Everybody, I was replying to someone elses question about number formatting and came up with a class that will format a number of any length with decimal or not, without rounding. It works perfectly, but after a comment by a noteworthy member my curiosity was aroused. Is there an easier/faster/better way to achieve this?? My Object: <?php /*GHETTO NUMBER FORMATTER v1.0*/ class ghettoNumber { private $input; private $output; public $whole; public $decimal; public $number; function __construct($input){ $this->input = $input; $this->output = $output; $this->number = $number; $this->whole = $whole; $this->decimal = $decimal; } private function ghettoFormat(){ //Split number at the .(decimal) $this->number = explode('.', $this->input); //Define the whole number $this->whole = $this->number[0]; //Format the whole number $this->whole = number_format($this->whole, 0,'', ','); //Define the decimal $this->decimal = $this->number[1]; //Format the decimal $this->decimal = rtrim($this->decimal, '0'); if(is_string($this->input)){ $this->output = $this->decimal != '' ? $this->whole.'.'.$this->decimal : $this->whole; } else{ $this->output = '<strong>ERROR:</strong> Input arg must be passed as type: <em>string</em>'; } //Return result return 'Original - '.$this->input.'<br> GhettoFormat - '.$this->output.' <br><br>'; } public function makeItGhetto(){ return $this->ghettoFormat(); } } ?> Sample Instantiation: $ghettoFormat = new ghettoNumber('12565456565.123401201001210000'); print $ghettoFormat->makeItGhetto(); I will reiterate my question once more... Is there, in your opinion, a BETTER way to achieve this (number formatting w/o rounding)? Thanks People, E Here is the number: $GrandTotal = "68.835"; $Deposit = round($GrandTotal , 2) * .5; echo $Deposit; I am looking for: 68.84 but the function doesn't seem to work right. Any help? I am very new to PHP and programming in general. About 3 weeks into this. I have this simple expression and some echo in my code. $x1 = $lat_b + $lat_a1; echo "<br>lat b: ".$lat_b; echo "<br>lat a1: ".$lat_a1; echo "<br>x1: ".$x1; $lat_b and $lat_a1 are derived from some xml extractions from a yahoo geocode api (that part works fine). The results of the expression and echos above are as follows: lat b: 40.402866 lat a1: 40.252590 x1: 80 Why is $x1 rounding automatically on me? Later, I substituted the expression above with $x1 = 40.402866 + 40.252590 ; The new result (shown below) makes sense to me. lat b: 40.402866 lat a1: 40.252590 x1: 80.655456 Hi fellas, Can anyone show me why my script is still rounding up / down? printf ("<tr> %s Avg gun Ahit %%</td> %s %.2f %%</td></tr>\n",$tdo,$tdor,number_format($ahit/$fired*100, 3, '.', '')); Do I have a syntax error as I cannot get my math equation to display more than 2 decimal places even though it's set to 3? Any help or advice would be really great. I need to round a number based on it's decimal. 3 or below should round down, 4 should round up to 5, 6 and 7 should round down to 5; 8 and 9 should round up to the next whole number. The below is returning 3 from 2.16. Code: [Select] <?php $a=2.16; $i=round($a, 1); $b=$i; $i=explode(".", $a); $decimal=$i[1]; if($decimal < 5){ $j=floor($b); $i= $decimal > 3 ? $j + .5 : $j; echo $i; } else{ $j=ceil($b); $i= $decimal < 8 ? $j - .5 : $j; echo $i; } ?> EDIT: The explode should have been on $i not $a. $f15 = 0.345677 (output from sql data pull) <?php echo number_format ($f15,3); ?> output = 0.345 <?php echo ltrim($f15,"0"); number_format ($f15,3); ?> output = .345677 How do I combine these so that I am BOTH rounding the result AND trimming a leading zero? Seems I can get one or the other, but not both. Suggestions on the syntax? Hello. This is my first post here. I am not an expert developer but I generally manage to do what is asked of me. However, now and then I'm stumped, like now... I have an online database system for our company in which staff can enter JOB related data, this includes 2 dimensions which they enter in millimetres (mm). Here's an example
$dimension1 might be entered as 850 Firstly, I need to round both dimensions up to the next 100mm so the 850 in this example would become 900 and the 1447 would become 1500. Once I've done that, I need to calculate the square metres (900mm x 1500mm / 1000 was my first thought) but that raised the second thing; adding a decimal point in the result. 900 x 1500 / 1000 = 1350 but I need the result to be 1.350 I know my way around basic PHP code but I'm no expert by any stretch. If anyone can help me understand how to get this calculation done I would be hugely appreciative. Thank you Martin |