Page 1 of 1

how to add decimal numbers using batch file?

Posted: 06 Feb 2013 02:37
by shirulkar
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

Re: how to add decimal numbers using batch file?

Posted: 06 Feb 2013 02:52
by foxidrive
Batch math is integer based.

You can use VBS...

Re: how to add decimal numbers using batch file?

Posted: 06 Feb 2013 04:52
by shirulkar
I have following code

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?

Posted: 06 Feb 2013 05:14
by foxidrive
As I replied earlier, you can use VBS.

Re: how to add decimal numbers using batch file?

Posted: 06 Feb 2013 06:37
by shirulkar
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?

Posted: 06 Feb 2013 06:56
by foxidrive
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?

Posted: 06 Feb 2013 07:50
by Squashman
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/

Re: how to add decimal numbers using batch file?

Posted: 06 Feb 2013 14:13
by !k

Re: how to add decimal numbers using batch file?

Posted: 06 Feb 2013 20:46
by Aacini
You may write a Batch file that 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?

Posted: 07 Feb 2013 08:39
by Aacini
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.

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
This is the output:

Code: Select all

6.9
1 / 3 with 9 decimals: 0.333333333

Antonio

Re: how to add decimal numbers using batch file?

Posted: 03 Jan 2023 18:53
by ValComputers
I tried to use this code for something I am trying to do and get the .XXX answer with the decimals in the wrong place

What I am trying to do ( and thought this would work for it ) is take a user input of a whole number ( any number entered ) Say 200 and then multiply that number by a percent ( 3.49% ) which is .0349 and then get that result and add another number 0.49 to that total to get the number I am looking for ? Anyone ?

Thanks

Chad

Re: how to add decimal numbers using batch file?

Posted: 03 Jan 2023 22:07
by Aacini
I copied the above code and adjusted it for your formula, that I think it is: result = 200 * 0.0349 + 0.49. Here it is:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

call :IntAsFP one=1.0000
call :IntAsFP number=200.0000
call :IntAsFP percent=0.0349
call :IntAsFP another=0.4900
set /A result=number*percent/one + another
call :IntToFP result=%result% 4
echo %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
The result is 7.4700 that I think is correct...

If you want to read the (whole) number from the user, just do this:

Code: Select all

set /P "number=Enter a whole number: "
call :IntAsFP number=%number%.0000
Antonio