Jump to content

Rounding down to nearest 5 or 0


zrweber

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/219424-rounding-down-to-nearest-5-or-0/
Share on other sites

Rounding down to the nearest multiple of 5?

function floor2nearest($number, $decimal) {
    return floor($number / $decimal) * $decimal;
}

echo floor2nearest(123, 5); // 120

echo floor2nearest(1.23, 0.33); // 0.99
echo floor2nearest(123.456, .999); // 122.877

 

By the way, you don't have to use a function. If you only need it in that one spot then just do the math inline.

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.