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

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

yodish

Posts: 22
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

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: 2775
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 147 times

### Re: finding difference between two numbers with commas/decim

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: 22
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

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: 2775
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 147 times

### Re: finding difference between two numbers with commas/decim

Enrico,

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: 22
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

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: 22
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

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
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: 2775
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 147 times

### Re: finding difference between two numbers with commas/decim

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: 22
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

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: 264 times

### Re: finding difference between two numbers with commas/decim

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