Akshayanti
Akshayanti

Reputation: 326

Number limit supported by batch files

I was trying to add a 12-digit and an 8-digit number using a batch file. However, it gives the following error- Invalid number. Numbers are limited to 32-bits of precision.

What should I do to overcome this obstacle?

If it helps, I was adding them like this-

set /a z= (%r:~0% + %u:~0%)

where r and u contain the numbers to be added in string format.

Upvotes: 1

Views: 2387

Answers (1)

Joey
Joey

Reputation: 354694

A 12-digit number is already outside the boundaries of Int32, which means you just cannot do arithmetic with it in batch files directly.

What you can do, is adding the numbers by digit, maintaining a carry, just as you did in primary school. It will be slower, but it works.

Another option is to let other programs do the calculation, e.g. PowerShell:

for /f %%x in ('powershell %r:~0% + %u:~0%') do set result=%%x

Side note: %r:~0% means, I think, »take a substring of %r%, starting at character 0, extending to the end of the string«, which is exactly the same as the string itself.

Upvotes: 3

Related Questions