Jump to content

Cureency Rounding


irebmy

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/235788-cureency-rounding/
Share on other sites

A function I came with.

function halfRound($num)
{
    // round number to nearest tenth
    $wR = round($num, 1);
    // get the difference
    $dif = $wR - $num;

    // check difference
    if($dif < 0)
        $num = $wR + 0.05; // add 0.05 to rounded number
    elseif($dif > 0)
        $num = $wR - 0.05; // take 0.05 to rounded number

    return $num;
}

$array = array(0.11, 0.28, 0.71, 10002.71);

foreach($array as $item)
{
    echo "<p>$item rounded to " . halfRound($item) . "</p>";
}

Link to comment
https://forums.phpfreaks.com/topic/235788-cureency-rounding/#findComment-1212041
Share on other sites

A function I came with.

function halfRound($num)
{
    // round number to nearest tenth
    $wR = round($num, 1);
    // get the difference
    $dif = $wR - $num;

    // check difference
    if($dif < 0)
        $num = $wR + 0.05; // add 0.05 to rounded number
    elseif($dif > 0)
        $num = $wR - 0.05; // take 0.05 to rounded number

    return $num;
}

$array = array(0.11, 0.28, 0.71, 10002.71);

foreach($array as $item)
{
    echo "<p>$item rounded to " . halfRound($item) . "</p>";
}

 

 

nice function...

you solved it thanks man.. :)

 

Link to comment
https://forums.phpfreaks.com/topic/235788-cureency-rounding/#findComment-1214598
Share on other sites

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.