finding difference between two numbers with commas/decimals



IBM's Command List programming language & Restructured Extended Executor

finding difference between two numbers with commas/decimals

Postby yodish » Fri Aug 07, 2015 12:03 am

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!
yodish
 
Posts: 23
Joined: Thu Oct 25, 2012 3:03 am
Has thanked: 0 time
Been thanked: 0 time

Re: finding difference between two numbers with commas/decim

Postby enrico-sorichetti » Fri Aug 07, 2015 1:56 am

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: finding difference between two numbers with commas/decim

Postby yodish » Fri Aug 07, 2015 6:21 pm

Thank you Enrico,

Now that d = 6000630.00
You wouldn't happen to know an easy way to put comma(s) back, would you?
yodish
 
Posts: 23
Joined: Thu Oct 25, 2012 3:03 am
Has thanked: 0 time
Been thanked: 0 time

Re: finding difference between two numbers with commas/decim

Postby enrico-sorichetti » Fri Aug 07, 2015 7:19 pm

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: finding difference between two numbers with commas/decim

Postby yodish » Fri Aug 07, 2015 11:02 pm

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!
yodish
 
Posts: 23
Joined: Thu Oct 25, 2012 3:03 am
Has thanked: 0 time
Been thanked: 0 time

Re: finding difference between two numbers with commas/decim

Postby yodish » Fri Aug 07, 2015 11:21 pm

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)
yodish
 
Posts: 23
Joined: Thu Oct 25, 2012 3:03 am
Has thanked: 0 time
Been thanked: 0 time

Re: finding difference between two numbers with commas/decim

Postby enrico-sorichetti » Sat Aug 08, 2015 1:53 am

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:
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: finding difference between two numbers with commas/decim

Postby yodish » Sat Aug 08, 2015 4:38 am

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.
yodish
 
Posts: 23
Joined: Thu Oct 25, 2012 3:03 am
Has thanked: 0 time
Been thanked: 0 time

Re: finding difference between two numbers with commas/decim

Postby BillyBoyo » Sat Aug 08, 2015 2:43 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: finding difference between two numbers with commas/decim

Postby delorimier » Sun Aug 09, 2015 5:25 am

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
delorimier
 
Posts: 9
Joined: Sat Aug 25, 2012 7:01 am
Has thanked: 0 time
Been thanked: 0 time


Return to CLIST & REXX

 


  • Related topics
    Replies
    Views
    Last post