Rounding issue

Public support forum for peer to peer support with related to the Visual Objects and Vulcan.NET products
BiggyRat

Rounding issue

Post 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
FFF
Posts: 1568
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Rounding issue

Post 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.
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
BiggyRat

Rounding issue

Post 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.
FFF
Posts: 1568
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Rounding issue

Post by FFF »

OK, then i misread your sample...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
BiggyRat

Rounding issue

Post 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...
User avatar
robert
Posts: 4470
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Rounding issue

Post by robert »

Jeff
Multiply by 2, round to 1 decimal and then divide by 2 ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Rounding issue

Post 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
BiggyRat

Rounding issue

Post 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
User avatar
robert
Posts: 4470
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Rounding issue

Post by robert »

Jeff
Did you try what I suggested?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

Rounding issue

Post 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))		        
Post Reply