Page 1 of 1

finding difference between two numbers with commas/decimals

PostPosted: Fri Aug 07, 2015 12:03 am
by yodish
Hello,
If
amount1 = 3,430,035.43 and
amount2 = 9,430,665.43
and I want to find the difference between these two numbers, why does the following code give me a 'Bad arithmetic conversion' error?
If Compare(amount1,amount2) /= 0 Then do
   Difference = amount2 - amount1
   say Difference
end


Thanks in advance!

Re: finding difference between two numbers with commas/decim

PostPosted: Fri Aug 07, 2015 1:56 am
by enrico-sorichetti
because Rexx does not understand the comma separating the thousandths
rexx only understands the decimal point.

also no reason to use the compare function

a = "3,430,035.43"
b = "9,430,665.43"

a = space(translate(a," ",","),0)
b = space(translate(b," ",","),0)

If a \= b Then do
   d = b - a
   say d
end

Re: finding difference between two numbers with commas/decim

PostPosted: Fri Aug 07, 2015 6:21 pm
by yodish
Thank you Enrico,

Now that d = 6000630.00
You wouldn't happen to know an easy way to put comma(s) back, would you?

Re: finding difference between two numbers with commas/decim

PostPosted: Fri Aug 07, 2015 7:19 pm
by enrico-sorichetti
quick and dirty,
ONLY FOR >0 NUMBERS

a = "3,430,035.43"
b = "9,430,665.43"

a = space(translate(a," ",","),0)
b = space(translate(b," ",","),0)

d = b - a

say left(d,         12) zformat(d)

say left(1,         12) zformat(1)
say left(999,       12) zformat(999)
say left(1000,      12) zformat(1000)
say left(10000,     12) zformat(10000)
say left(100000,    12) zformat(100000)
say left(1000000,   12) zformat(1000000)

exit

zformat:procedure
    parse arg N
    parse var N  I "." D .

    if  D = "" then ,
        F = ""
    else ,
        F = "." || D

    do  while ( I >= 1000 )

        F   = "," || right( I // 1000, 3, "0" ) || F
        I   = I % 1000
    end

    if  I = 0 then ,
        return substr(F, 2)
    else ,
        return I || F


6000630.00   6,000,630.00
1            1
999          999
1000         1,000
10000        10,000
100000       100,000
1000000      1,000,000

Re: finding difference between two numbers with commas/decim

PostPosted: Fri Aug 07, 2015 11:02 pm
by yodish
Enrico,
I appreciate your help.

I'm running into a stumbling block though. I need to be able to perform basic calculations on numbers. However, the solution we have been talking about assumes the numbers contain 2 commas.

Do you have any idea how I could modify the original code to account for no commas, 1 comma, 2 commas, or even 3 commas?

thanks!

Re: finding difference between two numbers with commas/decim

PostPosted: Fri Aug 07, 2015 11:21 pm
by yodish
I can't take credit for this, I found it in another thread; but, this seems to strip all commas, regardless of how many.
a = Space(Translate(a,,','),0)

Re: finding difference between two numbers with commas/decim

PostPosted: Sat Aug 08, 2015 1:53 am
by enrico-sorichetti
Do you have any idea how I could modify the original code to account for no commas, 1 comma, 2 commas, or even 3 commas?


what the fluck do You think
a = space(translate(a," ",","),0)

does, if not getting rid of all the commas in the string ???

a = "3,430,035,430,035,430,035,430,035,430,035,430,035.43"
a1 = space(translate(a," ",","),0)
say "***** a1 " a1


I am well aware that
a = space(translate(a,,","),0)

does the same
but I prefer clarity to semi obfuscation ( for the people who have to maintain Your code )

looks like I wasted my time writing and testing my snippets
good luck I am calling myself out
:evil:

Re: finding difference between two numbers with commas/decim

PostPosted: Sat Aug 08, 2015 4:38 am
by yodish
I appreciate the help, i'm still wrapping my head around the code and hope to understand it better in the future. Have a nice weekend.

Re: finding difference between two numbers with commas/decim

PostPosted: Sat Aug 08, 2015 2:43 pm
by BillyBoyo
When looking at code you don't understand, you can use Trace in the code and experiment with different data. You need to understand code before you can use it. You need to attempt to understand it before asking further questions about it.

Re: finding difference between two numbers with commas/decim

PostPosted: Sun Aug 09, 2015 5:25 am
by delorimier
enrico-sorichetti wrote:looks like I wasted my time writing and testing my snippets


No you did not Enrico :-) I just used your zformat procedure for my personal use. I also learned from your small code.

Thanks.

Rene FERLAND, Montreal