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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.