xsharp.eu • Rounding issue - Page 2
Page 2 of 4

Rounding issue

Posted: Sun Jan 19, 2020 9:38 am
by BiggyRat
Thanks very much Karl-Heinz. I was thinking round to 2 decimal places because that's how dollars and cents work, so the logic of rounding to 1 place seems strange to me. But thanks very much for the explanation. I may not understand the logic, but I'll know how to use it in future! :D

Rounding issue

Posted: Sun Jan 19, 2020 9:46 am
by FFF
But according to #12740 you DON'T want dollars/cent, you want dollars/dime (is that the word?) That's the reason you need the "1", like K-H wrote. _Showing_ the trailing zero is a display thing, not a mathematical one.

Rounding issue

Posted: Sun Jan 19, 2020 9:59 am
by BiggyRat
No Karl, I needed the cents. In that example the rounding was to round to the next whole Dollar/Cent value e.g.
855 dollars, Zero cents. For example

855.01 is 855 dollars and 1 cent. We don't have 1 cent pieces any more. Our next valid D/C amount is 855.05 (5 cents) then 855.10 (10 cents) in other words it must end in 0, 5 or10. I hope that makes some sense.

Rounding issue

Posted: Sun Jan 19, 2020 10:35 am
by FFF
OK, then i misread your sample...

Rounding issue

Posted: Sun Jan 19, 2020 11:34 am
by BiggyRat
Am I correct in thinking this solution won't work for 5 cents (0.05)? I can live with it if that's the case...

Rounding issue

Posted: Sun Jan 19, 2020 1:03 pm
by robert
Jeff
Multiply by 2, round to 1 decimal and then divide by 2 ?

Robert

Rounding issue

Posted: Sun Jan 19, 2020 4:55 pm
by Karl-Heinz
BiggyRat wrote:Am I correct in thinking this solution won't work for 5 cents (0.05)? I can live with it if that's the case...
Hi Jeff,

what´s the problem with 0.05 ?

? Round ( 0.05 , 2 ) // ok 0.05
? Round ( 0.05 , 1 ) // ok 0.1

regards
Karl-Heinz

Rounding issue

Posted: Sun Jan 19, 2020 10:06 pm
by BiggyRat
Hi Karl-Heinz, 5 cents is 5 cents - accounting wise. If a user enters in a Dollar value and a Quantity, then the tax makes the amount end in 4 cents (0.04).

Round ( 0.04 , 2 ) // 0.04
Round ( 0.04 , 1 ) // 0.0

Where's the middle ground? It can only either be 10 cents or an even dollar, no 5 cents (and by 5 cents I'm including 15, 25, 35, 45...95 cents as well.)

How am I to code for this? How am I to know if something entered by the end user needs to be rounded to 2 or 1? Rounding by 1 means I could be out by as much as 6 cents per transaction, which over time throws all the figures out massively.

Regards,

Jeff

Rounding issue

Posted: Sun Jan 19, 2020 10:49 pm
by robert
Jeff
Did you try what I suggested?

Robert

Rounding issue

Posted: Sun Jan 19, 2020 11:27 pm
by Jamal
Don't use the Round function. Just chop off any decimal digits after the 2nd decimal position.

Code: Select all

FUNCTION NoRound(fValue AS USUAL) AS REAL8 PASCAL
   LOCAL c AS STRING             
   LOCAL dwDecPos AS DWORD  
   
   c := alltrim(AsString(fValue))   
   dwDecPos := At2(".", c)    
  
RETURN Val(SubStr3(c, 1, dwDecPos-1) + "." + SubStr3(c, dwDecPos+1,2))