how to add decimal numbers using batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

how to add decimal numbers using batch file?

#1 Post by shirulkar » 06 Feb 2013 02:37

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to add decimal numbers using batch file?

#2 Post by foxidrive » 06 Feb 2013 02:52

Batch math is integer based.

You can use VBS...

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: how to add decimal numbers using batch file?

#3 Post by shirulkar » 06 Feb 2013 04:52

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to add decimal numbers using batch file?

#4 Post by foxidrive » 06 Feb 2013 05:14

As I replied earlier, you can use VBS.

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: how to add decimal numbers using batch file?

#5 Post by shirulkar » 06 Feb 2013 06:37

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to add decimal numbers using batch file?

#6 Post by foxidrive » 06 Feb 2013 06:56

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: how to add decimal numbers using batch file?

#7 Post by Squashman » 06 Feb 2013 07:50

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/


Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to add decimal numbers using batch file?

#9 Post by Aacini » 06 Feb 2013 20:46

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to add decimal numbers using batch file?

#10 Post by Aacini » 07 Feb 2013 08:39

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

ValComputers
Posts: 6
Joined: 30 Dec 2022 19:47

Re: how to add decimal numbers using batch file?

#11 Post by ValComputers » 03 Jan 2023 18:53

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to add decimal numbers using batch file?

#12 Post by Aacini » 03 Jan 2023 22:07

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

Post Reply