ksthakur
15th July 2002, 14:47
Hi All,

I want to round amount as stated below:

Suppose the amount is 100.31

The desired output is 100.35

So the rounding to nearing 5 paise,

Please help!

Warm regards,
Kuldeep

evesely
15th July 2002, 18:41
Do you always want to round up to a 5 (and not a zero)? For example, does 100.36 round to 100.40 or 100.45? I'm going on the assumption that the latter is what you want. I don't have time to test this, but it might be a start. In the code below, value is the value in, and diga is the number of digits after the decimal point to which you want to round. Thus, your example would be round.to.five(100.31, 2):

function double round.to.five(double value, long diga)
{
double modifier

modifier = 5.0 * pow(10.0, -1.0 * diga)
return(round(value - modifier, diga - 1, 2) + modifier)
}


This works if diga is greater than or equal to 1. If you need to start rounding at or above whole numbers, you would need to change things a bit.

ksthakur
16th July 2002, 09:21
Thanks evesely

surely i would want to round off as per the following pattern:

131.31 => 131.35
131.36 => 131.40
131.98 => 132.00

So zero and five paise both can be as the last digit.

Please suggest, the solution above has produced results though!


regards,

Ilansu
16th July 2002, 16:06
It's a different approach but it work's as you wanted.
function round.sum.to.05(ref domain tcqiv1 number.to.modify)
{
string str.num(20)
string after.point(1)
long point

str.num=str$(number.to.modify)
point=pos(str.num,".")
after.point=str.num(point+2;1)

if val(after.point)>5 then
number.to.modify=val(str.num(1;point+1))+0.1
else
if val(after.point)<5 then
number.to.modify=val(str.num(1;point+1))
endif
endif
}

Ilan S

OmeLuuk
16th July 2002, 17:24
In the past I came across a rounding mechanism that was like this:
"If rounding must be done on the number that is exact between two possible values (Rounding Factor/2) always round to the nearest even multiplicand of the Rounding Factor"

This made rounding more fair because of sometimes rounding was done up and sometimes rounding was done down.

So in an example:
crnd = 1.000
Numbers to round = result (accumulated rounding error)
normal way: round up
0.5000000 = 1.000 (+0.5000000)
1.5000000 = 2.000 (+1.0000000)
2.5000000 = 3.000 (+1.5000000)
3.5000000 = 4.000 (+2.0000000)
4.5000000 = 5.000 (+2.5000000)
5.5000000 = 6.000 (+3.0000000)

alternative way: round to even
0.5000000 = 0.000 (-0.5000000)
1.5000000 = 2.000 (+0.0000000)
2.5000000 = 2.000 (-0.5000000)
3.5000000 = 4.000 (+0.0000000)
4.5000000 = 4.000 (-0.5000000)
5.5000000 = 6.000 (+0.0000000)

Seems fair to me, although it looks odd sometimes. Then I wonder, why don't we always round like this!?

evesely
16th July 2002, 18:25
This is a modification of my original solution to fit your REAL requirements:

function double round.to.five(double value, long diga)
{
double modifier
double tmp_val

modifier = 5.0 * pow(10.0, -1.0 * diga)
tmp_val = round(value, diga - 1, 2)
return(tmp_val - value >= modifier ? tmp_val - modifier : tmp_val)
}

Again, it works for diga >= 1.

Francesco
16th July 2002, 19:32
Seems fair to me, although it looks odd sometimes. Then I wonder, why don't we always round like this!?

What does fair have to do with it? In business practice rounding means 'a little more for me and a little less for you'. ;)

It reminds me of the story of one of the first arrested computer criminals (it was used in one of the Superman movies where he was played by Richard Prior), nicknamed 'The Cent Witch'.
This guy was working for a major bank (or credit company or something) and found out that interests were only rounded up to 1/10th of a cent. He made a few modifications to the software, so the 100ths and 1000ths pennies that were left behind in the routing would be routed to a special account.
Because of the transactional volume, these bits and pieces of electronic money added up real fast. What's more, at the time no one would have considered a scheme like this.

He probably would have lived long and very prosperous if he hadn't gotten into a spending habbit and showed up for work in his Ferrari one day. Naturally that's when he raised a little suspision and got himself arrested shortly after.

evesely, nice coding. A Baan salute to you.

ksthakur
18th July 2002, 06:59
Hi evesely,

Thanks a lot, it is really a good solution.

Kuldeep