zrweber Posted November 22, 2010 Share Posted November 22, 2010 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 More sharing options...
requinix Posted November 22, 2010 Share Posted November 22, 2010 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 https://forums.phpfreaks.com/topic/219424-rounding-down-to-nearest-5-or-0/#findComment-1137796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.