1748223 Members
4876 Online
108759 Solutions
New Discussion юеВ

handling 12 digit number

 
Praveen Bezawada
Respected Contributor

handling 12 digit number

Hi
I need to compare 12+ digits number in a shell script, but when i do normal comparison using -lt or -gt, I am not getting the correct result.
I am working on K380, using 64bit kernel
How to get around this problem
Thanks in advance
Praveen
3 REPLIES 3
Volker Borowski
Honored Contributor

Re: handling 12 digit number

Hi Praveen,

with such long numbers, you have to convert to strings. I attached a SIMPLE c-program, doing it. It does not validate parameters, esp. length, just fills in leading zeros and does a string compare.

Sample compilation on HP-UX 11.0 included.
Since '-' is smaller than '0', it even works with negative numbers. It does not work with decimals, you might need to adjust it, if this is needed !!! Currently it is for a max of 29 digits, but you can modify it as you like.

Volker
A. Clay Stephenson
Acclaimed Contributor

Re: handling 12 digit number

Hi,

The following attachment will do the trick. It uses bc (binary calculator) to do unlimited precision comparisons.
Use it like this:
MYVAR=`bc.sh ${N1} ${N2}`

where N1 and N2 are the comparands

if N1 < N2, MYVAR will be set to -1
if N2 = N2, MYVAR will be set to 0
if N1 > N2, MYVAR will be set to 1

You can then use the value of MYVAR within your typical shell logic.

Regards, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: handling 12 digit number

Ooops, Remove the cp ${B1} /tmp/x.bc line from the attachment, it was there for testing.

Clay
If it ain't broke, I can fix that.