SET Command - Floating point numbers?
Asked Answered
S

3

6

How do I use the SET command in windows for floating point arithmatic. /A stands for arithmetic and %VAR% prints the data of VAR not the name.

For example when I do:

SET /A VAR="2.5+3.1"
ECHO %VAR%
pause

I get the error: 'Missing Operator'. The output (5.6) should be converted to floating point too

I'm still busy learning the basic syntax.

Regards, Snipe

Stormy answered 20/9, 2014 at 16:59 Comment(0)
C
7

The arithmetic operations of SET /A command are performed over 32-bits integer numbers only; however, you may easily simulate fixed point operations with SET /A if you choose a number of decimal digits and preserve they throughout the operations. For example:

REM Select two decimal digits for all operations
SET /A VAR=250+310
ECHO RESULT: %VAR:~0,-2%.%VAR:~-2%

This method allows you to directly perform addition or subtraction of two "Fixed Point" numbers. Of course, if you want to input numbers in the usual way (with decimal point), you must eliminate the decimal point and left zeros before perform the operations.

You may also directly multiply or divide a FP (fixed point) number by an integer and the result is correct. To multiply or divide two FP numbers we may use an auxiliary variable called ONE with the correct number of decimal places. For example:

rem Set "ONE" variable with two decimal places:
SET ONE=100
rem To multiply two FP numbers, divide the result by ONE:
SET /A MUL=A*B/ONE
rem To divide two FP numbers, multiply the first by ONE:
SET /A DIV=A*ONE/B

Further details at this post.

Constantino answered 20/9, 2014 at 19:13 Comment(2)
Yeah its working. Can you please explain me this %VAR:~0,-2%.%VAR:~-2% ?? – Elnoraelnore
@METALHEAD that's basically doing a substring split on %VAR%. %VAR:~0,-2% is grabbing the entire string minus the last two chars. Then it puts a dot (the hacked together decimal). Then %VAR:~-2% grabs the last two chars of the string. Therefore "100" would become "1.00" or "99999" becomes "999.99" – Circumsolar
M
5

you cannot do this with batch.It works only with integers. But you can use Jscript or powershell:

>powershell 5.6+3.1
8.7

For jscript you'll need to create aditional bat and call it (e.g. jscalc.bat):

 @if (@x)==(@y) @end /***** jscript comment ******
     @echo off

     cscript //E:JScript //nologo "%~f0"  %*
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/


 WScript.Echo(eval(WScript.Arguments.Item(0)));

Example:

>jscalc.bat "4.1+4.3"
8.4

have on mind that the jscript above is not pretty robust and will not handle bad formated expression.

To set the result into variable in both cases you'll need an wrapping FOR /F:

>for /f "delims=" %# in ('powershell 5.6+3.1') do set result=%#

>set result=8.7

>for /f "delims=" %# in ('jscalc 5.6+3.1') do set result=%#

>set result=8.7

Most probably you have installed powershell , but it's not available on all machines (by default is not installed on Vista,XP,2003) so if you don't have you'll need the jscalc.bat

And also you can use jscript.net (the ANOTHER jscript).Creating a .bat file that uses this is a little bit more verbose and creates a small .exe file but is an option too .Here's the jsnetcalc.bat:

@if (@X)==(@Y) @end /****** silent jscript comment ******

@echo off
::::::::::::::::::::::::::::::::::::
:::       compile the script    ::::
::::::::::::::::::::::::::::::::::::
setlocal


if exist "%~n0.exe" goto :skip_compilation

:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
    if exist "%%v\jsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxv\jsc.exe"
        goto :break_loop
    )
)
echo jsc.exe not found && exit /b 0
:break_loop



call %jsc% /nologo /out:"%~n0.exe" "%~f0" 
::::::::::::::::::::::::::::::::::::
:::       end of compilation    ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation

::
::::::::::
"%~n0.exe" %*
::::::::
::
endlocal
exit /b 0

****** end of jscript comment ******/

import System;

var arguments:String[] = Environment.GetCommandLineArgs();
Console.WriteLine( eval(arguments[1]) );

Example:

>jsnetcalc.bat 4.5+7.8
12.3

And even one more way that uses MSHTA (in expression set the expression you want to calculate):

@echo off
setlocal

:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(eval("
set "endJS=)));""

set "expression=8.5+3.5"

:: FOR /F does not need pipe
for /f %%N in (
  '%beginJS% %expression% %endJS%'
) do set result=%%N

echo result=%result%

Can be edited to accept arguments like the examples above also...

Merrygoround answered 20/9, 2014 at 17:6 Comment(0)
B
1

After thinking about it: A simple calculation like 100 * 0.1 would be nice in batch. So I made a first little work-around with acceptable results.

Maybe someone like to improve it with more steps and share it with the stack community as I do now. Or find some bugs.

My script atm is not so big and maybe integratable without adding to many lines.

It's only working with single type of operation. The script decide which one it is or if they are mixed up.

My method is to make integers of the calculation fetching the highest number of digits and fill up if necessary with ceros and (afterwards / before) remove redundant ceros.

@ECHO OFF

REM -> Seems to be a good approach to change directory to a safe folder...
SET "CurrentFolder=%CD%"
CD /D "%temp%"

SETLOCAL EnableExtensions EnableDelayedExpansion

ECHO/
SET CALC=10/2.5
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  4
ECHO/    Result:  !Result!

ECHO/
SET CALC=2.5/000010
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  0.25
ECHO/    Result:  !Result!

ECHO/
SET CALC=10/0.99
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  10.10101010101010101010101010101
ECHO/    Result:  !Result!

ECHO/
SET CALC=0.7/0.38
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  1.8421052631578947368421052631579
ECHO/    Result:  !Result!

ECHO/
SET CALC=15641/3.0810
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  5076.5985069782538136968516715352
ECHO/    Result:  !Result!

ECHO/
SET CALC=5 / 0.0003
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  16666.666666666666666666666666667
ECHO/    Result:  !Result!

ECHO/
SET CALC=1234*0.00008
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  0.09872
ECHO/    Result:  !Result!

ECHO/
SET CALC=-515415+987165.001
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  471750.001
ECHO/    Result:  !Result!

ECHO/
SET CALC=515415-515414.010
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  0.99
ECHO/    Result:  !Result!

ECHO/
SET CALC=515415.09-515414.0001
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  1.0899
ECHO/    Result:  !Result!

ECHO/
SET CALC=5415.09-5414.0001
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  1.0899
ECHO/    Result:  !Result!

ECHO/
SET CALC=5415.09-15414.0001
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  -9998.9101
ECHO/    Result:  !Result!

ECHO/
SET CALC=300 * 0.04
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  12
ECHO/    Result:  !Result!

ECHO/
SET CALC=5414.09-5414.0001
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  0.0899
ECHO/    Result:  !Result!

ECHO/
ECHO -------------------------------------------------
ECHO      Result * 105.3
ECHO -------------------------------------------------
SET CALC=!Result! * 105.3
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat Result "!CALC!"
ECHO/  Pre-Calc:  ?
ECHO/    Result:  !Result!
ECHO -------------------------------------------------

ECHO/
SET CALC=5414.09-5414.0001 * 42
ECHO/  Original:  !CALC!
CALL :HandleFloat "%CALC%" CALCDEF DIGITS
ECHO/ DeFloated:  !CALCDEF!
ECHO/    Digits:  !DIGITS!
CALL :CalcFloat NoResult "!CALC!"
ECHO/  Pre-Calc:  What was the question?
ECHO/    Result:  !NoResult!


GOTO :EndOfBatch



REM -> CALL :CalcFloat OUTPUTVAR "Calculation" (PRECISION)
REM -> Allows calculations with floating point numbers. E.g.:
REM -> :CalcFloat OUTPUTVAR "7/2" 2 would set OUTPUTVAR to 3.5
REM -> You cannot mix multiplication or division with addition or subtraction (for now)!
REM <- Default value for PRECISION is 2.
REM -> 2147483647+1 = -2147483648 <-> 2147483647+2 = -2147483647 <-> 2147483647+3 = -2147483646 ...
:CalcFloat
IF "%~1" == "" EXIT /B 1
IF "%~2" == "" EXIT /B 1
CALL :HandleFloat "%~2" DeFloated NumDigits
SET /A CalcFloatPrecision=2
IF NOT "%~3" == "" (
   SET /A CalcFloatPrecision=%~3
) ELSE IF "!NumDigits!" GEQ "4" (
   SET /A CalcFloatPrecision=1
)
CALL :StrLen DeFloatedLen DeFloated
SET CalcFloatMode=
FOR /L %%I IN (0,1,%DeFloatedLen%) DO (
   IF "!DeFloated:~%%I,1!" == "-" (
      IF "!CalcFloatMode!" == "SCALE" GOTO :CalcFloatConfused
      IF "!CalcFloatMode!" == "MULTI" GOTO :CalcFloatConfused
      SET CalcFloatMode=BASIC
   ) ELSE IF "!DeFloated:~%%I,1!" == "+" (
      IF "!CalcFloatMode!" == "SCALE" GOTO :CalcFloatConfused
      IF "!CalcFloatMode!" == "MULTI" GOTO :CalcFloatConfused
      SET CalcFloatMode=BASIC
   ) ELSE IF "!DeFloated:~%%I,1!" == "/" (
      IF "!CalcFloatMode!" == "BASIC" GOTO :CalcFloatConfused
      IF "!CalcFloatMode!" == "MULTI" GOTO :CalcFloatConfused
      SET CalcFloatMode=SCALE
   ) ELSE IF "!DeFloated:~%%I,1!" == "*" (
      IF "!CalcFloatMode!" == "BASIC" GOTO :CalcFloatConfused
      IF "!CalcFloatMode!" == "SCALE" GOTO :CalcFloatConfused
      SET CalcFloatMode=MULTI
   )
)
IF NOT DEFINED CalcFloatMode SET CalcFloatMode=SCALE
IF "!CalcFloatMode!" == "BASIC" (
   SET /A CalcFloatBaseResult=!DeFloated!
   IF "%NumDigits%" GTR "0" (
      CALL :StrLen CalcFloatBaseResultLen CalcFloatBaseResult
      IF "!CalcFloatBaseResultLen!" LEQ "!NumDigits!" (
         FOR /L %%i IN (1,1,%NumDigits%) DO IF "%%i" GEQ "!CalcFloatBaseResultLen!" SET CalcFloatBaseResult=0!CalcFloatBaseResult!
      )
      SET CalcFloatBaseResult=!CalcFloatBaseResult:~0,-%NumDigits%!.!CalcFloatBaseResult:~-%NumDigits%!
   )
   SET "%~1=!CalcFloatBaseResult!"
) ELSE IF "!CalcFloatMode!" == "MULTI" (
   SET /A CalcFloatBaseResult=!DeFloated!
   SET /A NumDigits=!NumDigits! * 2
   IF "!NumDigits!" GTR "0" (
      CALL :StrLen CalcFloatBaseResultLen CalcFloatBaseResult
      IF !CalcFloatBaseResultLen! LSS !NumDigits! (
          FOR /L %%i IN (0,1,!NumDigits!) DO IF !CalcFloatBaseResultLen! LEQ %%i SET CalcFloatBaseResult=0!CalcFloatBaseResult!
      )
      FOR /F "tokens=1" %%F IN ("!NumDigits!") DO SET CalcFloatBaseResult=!CalcFloatBaseResult:~0,-%%F!.!CalcFloatBaseResult:~-%%F!
   )
   SET "%~1=!CalcFloatBaseResult!"
) ELSE (
   SET CalcFloatScale=1
   SET CalcFloatVal=
   FOR /L %%i IN (1,1,%CalcFloatPrecision%) DO SET /A CalcFloatScale*=10
   SET /A CalcFloatVal=!CalcFloatScale! * !DeFloated!
   SET /A CalcFloatVal1=!CalcFloatVal! / !CalcFloatScale!
   SET /A CalcFloatVal2=!CalcFloatVal! - !CalcFloatVal1! * !CalcFloatScale!
   SET "%~1=!CalcFloatVal1!.!CalcFloatVal2!"
)
CALL :StrLen FinalCalcLen %~1
FOR /L %%i IN (0,1,!FinalCalcLen!) DO IF "!%~1:~%%i,1!" == "." GOTO :CalcFloatRemoveCero
EXIT /B 0
:CalcFloatRemoveCero
SET /A FinalCalcLen=!FinalCalcLen! - 1
FOR /F "tokens=1" %%F IN ("!FinalCalcLen!") DO IF NOT "!%~1:~%%F,1!" == "." IF "!%~1:~%%F,1!" == "0" (
   SET "%~1=!%~1:~0,-1!
   GOTO :CalcFloatRemoveCero
)
IF "!%~1:~-1!" == "." SET "%~1=!%~1:~0,-1!
EXIT /B 0
:CalcFloatConfused
ECHO Well multiplication or division do not work together with addition or subtraction atm.
EXIT /B 1



REM -> CALL :HandleFloat "INPUT" OUTPUT (DIGITS)
REM -> Turns all floating numbers into integers.
REM <- INPUT: The calculation as sting.
REM <- OUTPUT: The variable which will hold the calculation without floating numbers.
REM <- DIGITS: Optional, the variable which will hold the number of maximum digits.
:HandleFloat
IF "%~2" == "" EXIT /B 1
SET "InputCalc=%~1"
IF "!InputCalc!" == "" EXIT /B 1
REM -> Stage I: Get the maximum digits.
CALL :StrLen InputCalcLen InputCalc
IF %InputCalcLen% EQU 0 EXIT /B
SET /A InputCalcNumberDigits=0
SET /A InputCalcNumberDigitsPrev=0
SET "InputCalc=!InputCalc!#"
FOR /L %%I IN (0,1,%InputCalcLen%) DO (
   SET InputCalcCharIsPoint=FALSE
   IF "!InputCalc:~%%I,1!" == "." (
      SET InputCalcCharIsPoint=TRUE
   ) ELSE IF "!InputCalc:~%%I,1!" == "," (
      SET InputCalcCharIsPoint=TRUE
   )
   IF "!InputCalcCharIsPoint!" == "TRUE" SET NumbersExpected=TRUE
   SET InputCalcCharIsNum=FALSE
   IF "!InputCalc:~%%I,1!" == "0" (
      SET InputCalcCharIsNum=TRUE
   ) ELSE (
      SET /A InputCalcCharIsNumTest=0
      SET /A InputCalcCharIsNumTest=!InputCalc:~%%I,1! >NUL 2>&1
      IF "!InputCalcCharIsNumTest!" GTR "0" SET InputCalcCharIsNum=TRUE
   )
   IF "!InputCalcCharIsNum!" == "TRUE" (
      IF "!NumbersExpected!" == "TRUE" (
         SET /A InputCalcNumberDigitsPrev=!InputCalcNumberDigitsPrev!+1
      )
   ) ELSE (
      IF "!InputCalcCharIsPoint!" == "FALSE" (
         SET NumbersExpected=FALSE
         IF "!InputCalcNumberDigitsPrev!" GTR "!InputCalcNumberDigits!" SET /A InputCalcNumberDigits=!InputCalcNumberDigitsPrev!
         SET /A InputCalcNumberDigitsPrev=0
      )
   )
)
IF NOT "%~3" == "" SET "%~3=!InputCalcNumberDigits!"
REM -> Stage II: Adjust numbers.
IF !InputCalcNumberDigits! GTR 0 (
   SET NewInputCalc=
   SET NewInputCalcFloatPart=
   SET InputCalcPrevCharWasNum=FALSE
   SET RedudantCero=FALSE
   FOR /L %%I IN (0,1,%InputCalcLen%) DO (
      SET InputCalcCharIsPoint=FALSE
      IF "!InputCalc:~%%I,1!" == "." (
         SET InputCalcCharIsPoint=TRUE
      ) ELSE IF "!InputCalc:~%%I,1!" == "," (
         SET InputCalcCharIsPoint=TRUE
      )
      IF "!InputCalcCharIsPoint!" == "TRUE" SET NumbersExpected=TRUE
      SET InputCalcCharIsNum=FALSE
      IF "!InputCalc:~%%I,1!" == "0" (
         IF NOT "!NumbersExpected!" == "TRUE" (
            IF NOT "!InputCalcPrevCharWasNum!" == "TRUE" (
               SET RedudantCero=TRUE
               SET InputCalcCharIsNum=FALSE
            )
         )
         IF NOT "!RedudantCero!" == "TRUE" SET InputCalcCharIsNum=TRUE
      ) ELSE (
         SET /A InputCalcCharIsNumTest=0
         SET /A InputCalcCharIsNumTest=!InputCalc:~%%I,1! >NUL 2>&1
         IF "!InputCalcCharIsNumTest!" GTR "0" SET InputCalcCharIsNum=TRUE
      )
      IF NOT "!RedudantCero!" == "TRUE" (
         IF "!InputCalcCharIsNum!" == "TRUE" (
            IF "!NumbersExpected!" == "TRUE" (
               SET "NewInputCalcFloatPart=!NewInputCalcFloatPart!!InputCalc:~%%I,1!"
            ) ELSE (
               SET "NewInputCalc=!NewInputCalc!!InputCalc:~%%I,1!"
            )
         ) ELSE (
            IF "!InputCalcCharIsPoint!" == "FALSE" (
               SET NumbersExpected=FALSE
               IF DEFINED NewInputCalcFloatPart (
                  SET /A NewInputCalcFloatPartIsCero=!NewInputCalcFloatPart! >NUL 2>&1
                  IF "!NewInputCalcFloatPartIsCero!" == "0" (
                     SET InputCalcPrevCharWasNum=TRUE
                     SET NewInputCalcFloatPart=
                  )
               )
               IF DEFINED NewInputCalcFloatPart (
                  CALL :HandleFloatBuildFloat NewInputCalc NewInputCalcFloatPart InputCalcNumberDigits
                  SET NewInputCalcFloatPart=
               ) ELSE IF "!InputCalcPrevCharWasNum!" == "TRUE" (
                  FOR /L %%I IN (1,1,!InputCalcNumberDigits!) DO SET "NewInputCalc=!NewInputCalc!0"
               )
               SET "NewInputCalc=!NewInputCalc!!InputCalc:~%%I,1!"
            )
         )
      )
      SET RedudantCero=FALSE
      SET InputCalcPrevCharWasNum=!InputCalcCharIsNum!
   )
   CALL :StrLen NewInputCalcLen NewInputCalc
   SET /A NewInputCalcLen=!NewInputCalcLen! - 1
   FOR /F "tokens=1" %%F IN ("!NewInputCalcLen!") DO SET "%~2=!NewInputCalc:~0,%%F!"
) ELSE (
   SET "%~2=%~1"
)
EXIT /B 0
:HandleFloatBuildFloat
CALL :StrLen NewInputCalcFloatPartLen %~2
SET NewInputCalcFloatPartRedudantCero=FALSE
SET /A CurrentNewInputCalcLen=0
IF "!%~2:~0,1!" == "0" (
   IF "!%~1!" == "" SET NewInputCalcFloatPartRedudantCero=TRUE
   IF "!NewInputCalcFloatPartRedudantCero!" == "FALSE" (
      SET NewInputCalcCharIsNumTest=
      SET /A NewInputCalcCharIsNumTest=!%~1:~-1! >NUL 2>&1
      IF NOT DEFINED NewInputCalcCharIsNumTest SET NewInputCalcFloatPartRedudantCero=TRUE
   )
   IF "!NewInputCalcFloatPartRedudantCero!" == "FALSE" CALL :StrLen CurrentNewInputCalcLen NewInputCalc
)
:HandleFloatBuildFloatGetLastNonFloat
IF "!CurrentNewInputCalcLen!" GTR "0" (
   SET /A CurrentNewInputCalcLen=!CurrentNewInputCalcLen! - 1
   FOR /F "tokens=1" %%F IN ("!CurrentNewInputCalcLen!") DO (
      IF "!%~1:~%%F,1!" GEQ "0" IF "!%~1:~%%F,1!" LEQ "9" (
         SET NewInputCalcFloatPartRedudantCero=FALSE
         IF "!%~1:~%%F,1!" == "0" SET NewInputCalcFloatPartRedudantCero=TRUE
         GOTO :HandleFloatBuildFloatGetLastNonFloat
      )
   )
)
FOR /L %%I IN (0,1,!%~3!) DO (
   SET SkipCurrentNumber=FALSE
   IF "!NewInputCalcFloatPartRedudantCero!" == "TRUE" (
      IF "!%~2:~%%I,1!" == "0" (
         SET SkipCurrentNumber=TRUE
      ) ELSE (
         SET NewInputCalcFloatPartRedudantCero=FALSE
      )
   )
   IF "!SkipCurrentNumber!" == "FALSE" (
      IF "%%I" GTR "!NewInputCalcFloatPartLen!" (
         SET "%~1=!%~1!0"
      ) ELSE (
         SET "%~1=!%~1!!%~2:~%%I,1!"
      )
   )
)
EXIT /B



REM -> CALL :StrLen CountVar StringVar
REM -> Sets CountVar var to the number of characters of the given StringVar
:StrLen
(
   IF "%~1" == "" EXIT /B 1
   (SET^ tmp=!%~2!)
   IF DEFINED tmp (
      SET /A "len=1"
      FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (
         IF "!tmp:~%%P,1!" NEQ "" (
            SET /A "len+=%%P"
            SET "tmp=!tmp:~%%P!"
         )
      )
   ) ELSE (
      SET /A "len=0"
   )
   SET /A "%~1=!len!"
   EXIT /B 0
)



REM -> Resets
:EndOfBatch
ECHO.
ECHO Exiting batch now...
ECHO.
ping 1.2.3.4 -n 1 -w 200 >NUL
ENDLOCAL
CD /D "%CurrentFolder%"
ping 1.2.3.4 -n 1 -w 100 >NUL
GOTO :Exit



:Exit


Results:
  Original:  10/2.5
 DeFloated:  100/25
    Digits:  1
  Pre-Calc:  4
    Result:  4

  Original:  2.5/000010
 DeFloated:  25/100
    Digits:  1
  Pre-Calc:  0.25
    Result:  0.25

  Original:  10/0.99
 DeFloated:  1000/99
    Digits:  2
  Pre-Calc:  10.10101010101010101010101010101
    Result:  10.1

  Original:  0.7/0.38
 DeFloated:  70/38
    Digits:  2
  Pre-Calc:  1.8421052631578947368421052631579
    Result:  1.84

  Original:  15641/3.0810
 DeFloated:  156410000/30810
    Digits:  4
  Pre-Calc:  5076.5985069782538136968516715352
    Result:  5076.5

  Original:  5 / 0.0003
 DeFloated:  50000 / 3
    Digits:  4
  Pre-Calc:  16666.666666666666666666666666667
    Result:  16666.6

  Original:  1234*0.00008
 DeFloated:  123400000*8
    Digits:  5
  Pre-Calc:  0.09872
    Result:  0.09872

  Original:  -515415+987165.001
 DeFloated:  -515415000+987165001
    Digits:  3
  Pre-Calc:  471750.001
    Result:  471750.001

  Original:  515415-515414.010
 DeFloated:  515415000-515414010
    Digits:  3
  Pre-Calc:  0.99
    Result:  0.99

  Original:  515415.09-515414.0001
 DeFloated:  5154150900-5154140001
    Digits:  4
Invalid number. Numbers are limited to 32-bits of precision.
  Pre-Calc:  1.0899
    Result:  0..99

  Original:  5415.09-5414.0001
 DeFloated:  54150900-54140001
    Digits:  4
  Pre-Calc:  1.0899
    Result:  1.0899

  Original:  5415.09-15414.0001
 DeFloated:  54150900-154140001
    Digits:  4
  Pre-Calc:  -9998.9101
    Result:  -9998.9101

  Original:  300 * 0.04
 DeFloated:  30000 * 4
    Digits:  2
  Pre-Calc:  12
    Result:  12

  Original:  5414.09-5414.0001
 DeFloated:  54140900-54140001
    Digits:  4
  Pre-Calc:  0.0899
    Result:  0.0899

-------------------------------------------------
     Result * 105.3
-------------------------------------------------
  Original:  0.0899 * 105.3
 DeFloated:  899 * 1053000
    Digits:  4
  Pre-Calc:  ?
    Result:  9.46647
-------------------------------------------------

  Original:  5414.09-5414.0001 * 42
 DeFloated:  54140900-54140001 * 420000
    Digits:  4
Well multiplication or division do not work together with addition or subtraction atm.
  Pre-Calc:  What was the question?
    Result:
Bondholder answered 14/9 at 20:1 Comment(2)
Nice pure batch solution – Cupric
Ty jeb. It's not perfect but it makes me happy to not have call ps or any other language... 😁 – Bondholder

© 2022 - 2024 β€” McMap. All rights reserved.