Welcome, Guest
Username: Password: Remember me
Visual Objects

Please use this forum to post questions about Visual Objects and Vulcan.NET
  • Page:
  • 1
  • 2

TOPIC:

Rounding issue 19 Jan 2020 05:47 #12735

  • BiggyRat
  • BiggyRat's Avatar
  • Topic Author



This code just will not round properly.

method EditFocusChange(oEditFocusChangeEvent) class InvoicingScr
local oControl as Control
local lGotFocus as logic
Local nGSTInc, nGSTPaid
oControl := IIf(oEditFocusChangeEvent == NULL_OBJECT, NULL_OBJECT, oEditFocusChangeEvent:Control)
lGotFocus := IIf(oEditFocusChangeEvent == NULL_OBJECT, FALSE, oEditFocusChangeEvent:GotFocus)
super:EditFocusChange(oEditFocusChangeEvent)
//Put your changes here
nGSTPaid := gGSTRate/100
nGSTInc := 1 + nGSTPaid
SetDecimal(2)
SetFixed(true)


IF AllTrim(self:oDCComboBoxEx11:CurrentItem) == "No GST"
self:oDCTOTPR1:CurrentText := Str(Round((((self:oDCQTY1:VALUE) * (self:oDCUNITPR1:VALUE)) *1),2))
self:oDCGST1:CurrentText := "0.00"
else
self:oDCTOTPR1:CurrentText := Str(Round(( ((self:oDCQTY1:VALUE) * (self:oDCUNITPR1:VALUE)) * nGSTInc),2),,2)
self:oDCGST1:CurrentText := Str(Round((((self:oDCQTY1:VALUE) * (self:oDCUNITPR1:VALUE))* nGSTPaid), 2),,2)
endif

? Round(((self:oDCQTY1:VALUE * self:oDCUNITPR1:VALUE) * nGSTInc),2)
? Str(Round(((self:oDCQTY1:VALUE * self:oDCUNITPR1:VALUE) * nGSTInc),2),,2)


SetFixed(.f.)
Return nil


Using the following variables for example:

GSTRate = 10
self:oDCQTY1:VALUE = 1
self:oDCUNITPR1:VALUE = 804.55

I keep getting 885.01 I need this to be 885.00 what am I missing please. The screenshot shows the Terminal Output for the code above:



Thanks again.
.
Attachments:

Please Log in or Create an account to join the conversation.

Rounding issue 19 Jan 2020 06:50 #12736

  • wriedmann
  • wriedmann's Avatar


  • Posts: 3297
  • Hi Jeff,
    my calculator gives me the following result:
    804.55 * 1.1 = 885,01
    The Windows calculator instead returns
    804.55 * 1.1 = 885.005
    VO is ok - but maybe you have other rules for tax calculations?
    Wolfgang
    P.S. why you don't type your variables nGSTInc, nGSTPaid?
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 07:01 #12738

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    Hi Wolfgang, I'm not disputing the correctness of the calculation the problem is to get 885.01 to ROUND to 885.00

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 07:13 #12739

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3297
  • Hi Jeff,
    ok, another question:
    885.005 should round to 885.00?
    885.006 should round to 885.01?
    Then it is easy:
    function JeffRound( nValue as float ) as float
    local nReturn as float
        
    nReturn := Round( nValue - 0.001, 2 )
    	
    return nReturn

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 07:29 #12740

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    Hi Wolfgang, no 885.01, 885.02, 885.03, 885.04 should all round DOWN to 885.00.

    885.05, 885.06, 885.07, 885.08, 885.09 should round UP to 885.10

    I'm afraid I tried your JeffRound function, and it gave the same result - 885.01

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 07:35 #12741

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3297
  • Hi Jeff,
    then you need to round to 1 decimal place and subtract the correction factor:
    Round( nValue - 0.01, 1 )
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 07:38 #12742

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    But what if the result in another transaction is 523.03? It should be 523.00, but If I understand your code correctly, it would give 523.02 wouldn't it?

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 07:43 #12743

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3297
  • I'm really, really, really sorry!
    If you don't understand my code, at least try it!
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Last edit: by wriedmann.

    Rounding issue 19 Jan 2020 08:16 #12744

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    My apologies Wolfgang, I meant no disrespect, of course I was going to try it, I was just thinking out loud. I don't get the 0.01 bit, as I thought the whole idea of the Round function was to do exactly what it said it would. I didn't think it required an undocumented "fudge factor" in order to make it work, WHICH YOUR CODE DID, So, thank you very very much again Wolfgang.

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 10:27 #12748

    • Karl-Heinz
    • Karl-Heinz's Avatar


  • Posts: 774
  • Hi Jeff,

    >> I keep getting 885.01 I need this to be 885.00 what am I missing please.

    you round to *two* decimal places,

    >> ? Round(((self:oDCQTY1:VALUE * self:oDCUNITPR1:VALUE) * nGSTInc),2 )

    but it must be one decimal place to get the desired results. Run this and you see the differences.
    LOCAL fValue AS FLOAT 
    LOCAL i AS DWORD  
    
    fValue := 885.00
    
    
    FOR i := 1 UPTO 9
    	
    	fValue += 0.01
    	
    	? Round ( fValue, 2 )  
    	? Round ( fValue, 1 )  
    	?	
    NEXT 	 

    regards
    Karl-Heinz

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 10:38 #12749

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    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

    Please Log in or Create an account to join the conversation.

    Last edit: by BiggyRat.

    Rounding issue 19 Jan 2020 10:46 #12750

    • FFF
    • FFF's Avatar


  • Posts: 1398
  • 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 (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Last edit: by FFF.

    Rounding issue 19 Jan 2020 10:59 #12752

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    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.

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 11:35 #12753

    • FFF
    • FFF's Avatar


  • Posts: 1398
  • OK, then i misread your sample...
    Regards
    Karl (X# 2.14.0.4; Xide 1.33; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 12:34 #12754

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    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...

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 14:03 #12757

    • robert
    • robert's Avatar


  • Posts: 3444
  • Jeff
    Multiply by 2, round to 1 decimal and then divide by 2 ?

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 17:55 #12760

    • Karl-Heinz
    • Karl-Heinz's Avatar


  • Posts: 774
  • 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

    Please Log in or Create an account to join the conversation.

    Rounding issue 19 Jan 2020 23:06 #12762

    • BiggyRat
    • BiggyRat's Avatar
    • Topic Author



    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

    Please Log in or Create an account to join the conversation.

    Last edit: by BiggyRat.

    Rounding issue 19 Jan 2020 23:49 #12763

    • robert
    • robert's Avatar


  • Posts: 3444
  • Jeff
    Did you try what I suggested?

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Rounding issue 20 Jan 2020 00:27 #12764

    • Jamal
    • Jamal's Avatar


  • Posts: 305
  • Don't use the Round function. Just chop off any decimal digits after the 2nd decimal position.
    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))		        

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1
    • 2