## how to add decimal numbers using batch file?

**Moderator:** DosItHelp

### how to add decimal numbers using batch file?

Hi

How to add decimal number in batch file.

For Example

a=2.4 and b=4.5 ,

c= a+b

=2.4+4.5

=6.9

I want result as 6.9 but i m getting it as 6

How to add decimal number in batch file.

For Example

a=2.4 and b=4.5 ,

c= a+b

=2.4+4.5

=6.9

I want result as 6.9 but i m getting it as 6

### Re: how to add decimal numbers using batch file?

Batch math is integer based.

You can use VBS...

You can use VBS...

### Re: how to add decimal numbers using batch file?

I have following code

it will open the calculator but i want out put without opening the calculator and calculator should take the input as given by me i.e "calc %num1%+1.2" . oR plzz suggest any alternative

Code: Select all

`SETLOCAL EnableExtensions`

FOR /F "usebackq tokens=*" %%f IN ("a.txt") DO CALL :runsox "%%f"

GOTO :EOF

:runsox

SET num1=%~1

calc %num1%+1.2

ENDLOCAL

it will open the calculator but i want out put without opening the calculator and calculator should take the input as given by me i.e "calc %num1%+1.2" . oR plzz suggest any alternative

### Re: how to add decimal numbers using batch file?

As I replied earlier, you can use VBS.

### Re: how to add decimal numbers using batch file?

Yes I did it using vbs. Can we give input to calculator using batch script? if yes then please tell me

### Re: how to add decimal numbers using batch file?

shirulkar wrote:Can we give input to calculator using batch script? if yes then please tell me

Nope. It doesn't give any options for command line use.

There's a command line calc here:

http://stevehanov.ca/blog/index.php?id=26

### Re: how to add decimal numbers using batch file?

Judago has a couple of scripts that can do floating point math in batch but it is probably much easier to create a vbscript on the fly to do the eval. If memory serves me correctly someone else posted a batch file that could do floating point math as well. I believe Judago even commented on it. Search the forums for it otherwise look at Judago's website.

http://judago.webs.com/

http://judago.webs.com/

### Re: how to add decimal numbers using batch file?

You may write a Batch file that

At Fractional (fixed point) operations using integer numbers post, I wrote:

For example:

I used this method to draw the Mandelbrot Set fractal graphic in text mode using fixed-point operations with 4 decimal digits:

Antonio

*split*the available digits of 32-bits signed numbers in integer and fractional parts in a very easy way; the only problem is that you must write numbers with the right number of decimal digits (unless you insert additional code to check and fix this point).At Fractional (fixed point) operations using integer numbers post, I wrote:

Aacini wrote:Arithmetic operations of SET /A command use 32-bits signed numbers with this range of values: -2147483648 to 2147483647. If a certain result requires less integer digits, the rest may be used for fractional part. To do that, just choose a number of fractional digits and preserve it throughout the operations.

These are the rules to achieve FixedPoint operations using integer numbers:

- "ONE" variable must contain a 1 followed by the right number of decimal zeros; this variable is used as the FP base value in several operations.
- To convert a (well-written) FP number to integer, just remove the decimal point and delete left zeros.
- If two FP numbers are added or subtracted, the result is correct.
- If a FP number is multiplied or divided by an integer, the result is correct.
- To multiply two FP numbers, divide the result by ONE: SET /A MUL=A*B/ONE.
- To divide two FP numbers, multiply the first by ONE: SET /A DIV=A*ONE/B.

For example:

Code: Select all

`@echo off`

setlocal EnableDelayedExpansion

call :IntAsFP a=2.4

call :IntAsFp b=4.5

set /A c=a+b

call :IntToFP c=%c% 1

echo %c%

goto :EOF

:IntAsFP Int=FP

set FP=%2

set %1=%FP:.=%

exit /B

:IntToFP FP=Int digits

set Int=%2

set %1=!Int:~0,-%3!.!Int:~-%3!

exit /B

I used this method to draw the Mandelbrot Set fractal graphic in text mode using fixed-point operations with 4 decimal digits:

Code: Select all

`set /A maxX=78, maxY=42, maxLevel=26, one=10000`

call :IntAsFP xLeft=-1.0000

call :IntAsFP yTop= 1.1250

call :IntAsFP xRight= 2.0000

call :IntAsFP yBottom=-1.1250

set /A xStep=(xRight-xLeft)/maxX, yStep=(yBottom-yTop)/maxY, four=4*one

rem etc, etc...

Antonio

### Re: how to add decimal numbers using batch file?

In the Batch file below I completed the FP conversion routines so they correctly manage fixed point numbers with negative sign and left zeros. I also added a second example.This is the output:

Antonio

Code: Select all

`@echo off`

setlocal EnableDelayedExpansion

call :IntAsFP a=2.4

call :IntAsFp b=4.5

set /A c=a+b

call :IntToFP c=%c% 1

echo %c%

call :IntAsFP one=1.000000000

set /A oneBy3=one/3

call :IntToFP result=%oneBy3% 9

echo 1 / 3 with 9 decimals: %result%

goto :EOF

:IntAsFP Int=FP

set %1=%2

set sign=

if "!%1:~0,1!" equ "-" (

set sign=-

set %1=!%1:~1!

)

set %1=!%1:.=!

:checkLeftZeros

if "!%1:~0,2!" neq "00" goto noLeftZeros

set %1=!%1:~1!

goto checkLeftZeros

:noLeftZeros

if "!%1:~0,1!" equ "0" if "!%1:~1,1!" neq "" set %1=!%1:~1!

set %1=%sign%!%1!

exit /B

:IntToFP FP=Int digits

set %1=%2

set sign=

if "!%1:~0,1!" equ "-" (

set sign=-

set %1=!%1:~1!

)

set %1=!%1:~0,-%3!.!%1:~-%3!

if "!%1:~0,1!" equ "." (

set %1=00000000!%1:~1!

set %1=0.!%1:~-%3!

)

set %1=%sign%!%1!

exit /B

Code: Select all

`6.9`

1 / 3 with 9 decimals: 0.333333333

Antonio