Jump to content

Recommended Posts

<?php
function ceiling($number, $significance = 1)
{
return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false;
}
echo ceiling(1.3, 0.5);  // 1.5
echo ceiling(1.7, 0.5);  // 2

 

Use your own formatting to format it

 

Hope this helps

Link to comment
https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059586
Share on other sites

Sure

function flooring($number, $significance = 1)
{
return ( is_numeric($number) && is_numeric($significance) ) ? (floor($number/$significance)*$significance) : false;
}
echo flooring(1.3, 0.5);  // 1
echo flooring(1.7, 0.5);  // 1.5

 

put in a simple inline code it would be

$value = 1.7;
echo sprintf("%01.2f", floor($value/0.5)*0.5);

Link to comment
https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059596
Share on other sites

Again, sorry this isn't right.

 

The first example rounds everything up, the second example rounds everything down.

 

I need it to round to the 'nearest'.

 

So 1.7 would be closer to 1.5 than 2, so would return 1.5.

 

1.8 would be closer to 2 than 1.5, so would return 2.

Link to comment
https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059600
Share on other sites

I think this is what you are after. It will round to the nearest value according to the $roundTo value passed

 

function roundToPartial($value, $roundTo)
{
    return round($value / $roundTo) * $roundTo;
}

echo roundToPartial(1.70, .5); //1.5
echo roundToPartial(1.80, .5); //2.0

Link to comment
https://forums.phpfreaks.com/topic/202056-round-to-nearest-50/#findComment-1059603
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.