Jump to content

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

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.