Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Rounding to nearest multiple of 0.5 keeping 0.25 as the deciding factor

Avatar

Level 1

Dear All,

How could I round amounts as illustrated by the following examples in Adobe Livecycle Designer?

8.06 = 8.00, 8.26 = 8.50, 8.74 = 8.50, 8.76 = 9.00

Thanks,

Sandeep

1 Reply

Avatar

Level 10

Hi Sandeep,

I don't think there's any trick to doing this, you just have to test and adjust the values yourself, something like;

app.alert(roundToNearestHalf(8.06));

app.alert(roundToNearestHalf(8.26));

app.alert(roundToNearestHalf(8.74));

app.alert(roundToNearestHalf(8.76));

function roundToNearestHalf(number)

{

    var result = Math.floor(number);

    var fraction = number - result;

    if (fraction >= 0.75)

    {

        result++;

    }

    else

    {

        if (fraction > 0.25)

        {

            result += 0.5;

        }

    }

    return result;

}

Regards

Bruce