batch file calculator with decimals

Hewlett-packard / Rc659aa-aba a1632x
July 10, 2009 at 13:30:26
Specs: Microsoft Windows Vista Home Premium, 2 GHz / 2045 MB
i made a calculator in a batch file using this

@echo off
title My Calculator
set /p math=Whatchoo gonna calculate?
set /a math2=%math%
echo %math% = %math2%
goto a

however, i soon discovered it didnt show
decimals. is there any way to do this??

See More: batch file calculator with decimals

Report •

July 10, 2009 at 14:00:38
Yes, by coding a lot of statement and using substring manipulation operators for formatting results.

NT batch math is aimed to perform simple operations on integers only. As I said you can spend a lot of coding to perform even floating point computing, but that is no worth.

In the Jurassic eve I coded such programs for ancient minicomputers and I can assure it is a hard work.

Report •

July 10, 2009 at 14:06:41
any idea where to start?

Report •

July 10, 2009 at 14:20:34
Start by learning NT batch math operators and string manipulation as explained by the quick (unfriendly) on-line help you get typing Set /? at prompt. Beware you are going onto the road to the hell.

Report •

Related Solutions

August 4, 2009 at 16:10:14

Its the basic version of my calculator.bat. It allows decimals.
Now Im improving the code. I want to improve the precision in very large and very small numerals...

:: Calculator.bat | Autor: Ricardo647 (Brazil) |
@echo off
setlocal enabledelayedexpansion
echo Calculadora.BAT
echo ----------------------------------------------------------
echo * The arithmetic expression must not contain spaces.
echo * Use dot to separate integers/decimals (no commas).
echo * Arithm operators: +-*/ Example: 123.55*868.33
echo ----------------------------------------------------------
echo To Quit: X + ENTER
set /p "expr=Type the expression: "
set r1=!expr:~0,1!
if %r1%==x goto fim
if %r1%==X goto fim

:Tamanho da expressao
set exprX=%expr%x

if not "!exprX:~%TamanhoExpr%,1!"=="" (
set /a TamanhoExpr += 1
goto TamanhoLoop

set /a TamanhoExpr=%TamanhoExpr%-1
set /a cont=-1
set sinal=indef
set def=nao
set op1=xxx
set PosicaoSinal=0

set /a cont=%cont%+1
if %cont% == %tam% goto prox
set dig=!expr:~%cont%,1!

if %dig%==+ if %sinal%==indef (
set sinal=adic
goto prox
if %dig%==- if %sinal%==indef (
set sinal=subtr
set def=sim
if %dig%==* if %sinal%==indef (
set sinal=mult
set def=sim
if %dig%==/ if %sinal%==indef (
set sinal=div
set def=sim
if %def%==sim goto prox
goto calc
goto prox

set /a ps=%cont%+1
set /a pss=%ps%-1
set /a tam1=%cont%
set /a tam2=%TamanhoExpr%-%tam1%-1
set op1=!expr:~0,%cont%!
set op2=!expr:~%ps%,%tam2%!
set opx=!expr:~%pss%,1!
set /a p1=0
set /a cpt=0

::removendo ponto decimal
set /a cpt=%cpt%+1
if %cpt%==%tam1% goto prox2
set sub1=!op1:~%cpt%,1!
if %sub1%==. goto prox2
goto remov1

set /a p1=%cpt%+1
set /a p2=0
set /a cpt=0

set /a cpt=%cpt%+1
if %cpt%==%tam2% goto prox3
set sub2=!op2:~%cpt%,1!
if %sub2%==. goto prox3
goto remov2

set /a p2=%cpt%+1
set string1=%op1%00000
set string2=%op2%00000
set /a casas1=2
set /a casas2=2
if %sinal%==mult (
set /a casas1=1
set /a casas2=1

if %sinal%==div (
set /a casas1=1
set /a casas2=1

if p1==%tam1% goto prox4
set string1=!string1:.=!

if p2==%tam2% goto x1000
set string2=!string2:.=!

set /a str1=%p1%+%casas1%
set string1=!string1:~0,%str1%!
set /a str2=%p2%+%casas2%
set string2=!string2:~0,%str2%!
set /a virg1=1
set /a virg2=1
::prevenindo No.Octal (0500 p.ex)
set oc=!string1:~0,1!
if %oc%==0 (
set string1=!string1:~1,15!
set /a virg1=%virg1%+1
goto octal1
) else goto octal2

set oc=!string2:~0,1!
if %oc%==0 (
set string2=!string2:~1,15!
set /a virg2=%virg2%+1
goto octal2
) else goto prox5

set /a val1=%string1%
set /a val2=%string2%
if %sinal%==adic goto adic
if %sinal%==subtr goto subtr
if %sinal%==mult goto mult
if %sinal%==div goto div

set /a result=%val1%+%val2%
goto ponto

set /a result=%val1%-%val2%
goto ponto

set /a y1=%val1%/100
set /a y2=%val2%/100
set /a prov1=%y1%*%y2%
set /a w1=%y1%*100
set /a w2=%y2%*100
set /a z1=%val1%-%w1%
set /a z2=%val2%-%w2%
set /a r1=%z1%*%w2%
set /a r2=%z2%*%w1%
set /a prov2=%r1%+%r2%
set /a rest=0
if not %prov2%==0 set rest=!prov2:~1,3!
if not %prov2%==0 set /a rest=%rest%*10
set /a prov2=%prov2%/10000
set /a prov3=%z1%*%z2%
set /a prov3=%prov3%+%rest%
set /a resultado=%prov1%+%prov2%
set prov3=!prov3:~0,3!
set resultado=%resultado%.%prov3%
goto resul

set oc=%val1%00
set /a val1=%oc%
set /a result=%val1%/%val2%
if %result% gtr 9 goto ponto
set fu=0.0
if %result%==0 set fu=0.00
set /a val1=%val1%*100
set /a result=%val1%/%val2%
set resultado=%fu%%result%
goto resul

set result=%result%

if not "!result:~%TamanhoRes%,1!"=="" (
set /a TamanhoRes += 1
goto virgLoop

set /a cpt=%TamanhoRes%-3
if %sinal%==div set /a cpt=%cpt%+1
set /a oc=%cpt%
set resultado=!result:~0,%cpt%!.!result:~%oc%,3!

set fu=!resultado:~0,1!
if %fu%==. set resultado=0%resultado%
set /a result=resultado
if %result% gtr 2146999999 set resultado=%resultado%... ERROR: too large numeral...
echo Result: %resultado%
set /p "oc= Press any key..."
set expr=
set TamanhoExpr=
set cont=
set sinal=
set def=
set op1=
set op2=
set opx=
set PosicaoSinal=
set dig=
set ps=
set pss=
set tam1=
set tam2=
set p1=
set p2=
set cpt=
set sub1=
set sub2=
set string1=
set string2=
set casas1=
set casas2=
set str1=
set str2=
set virg1=
set virg2=
set oc=
set result=
set resultado=
set y1=
set y2=
set prov1=
set prov2=
set prov3=
set w1=
set w2=
set z1=
set z2=
set r1=
set r2=
set rest=
set val1=
set val2=
goto inic

Report •

August 4, 2009 at 16:55:27
>> any idea where to start?

the next best thing you can use, natively, is vbscript. Else, use a real programming language, like Python, or Perl.

GNU win32 packages | Gawk

Report •

August 8, 2009 at 12:35:12
wow. thats an epic code.

Report •

August 8, 2009 at 14:02:30
Here's a perl solution.


use strict;
use warnings;

print "Whatchoo gonna calculate? ";
chomp(my $calc = <STDIN>);

my $answer = eval $calc;
print $answer, $/;

Report •

August 8, 2009 at 19:43:41
>>Here's a perl solution.

though its workable, however, still need input sanitizing
(eg, check for numbers...etc)

# ./
Whatchoo gonna calculate? system("ls")

that said, here's an equivalent Python solution

choice = raw_input("what you want to calculate")
print eval(choice,{},{})

GNU win32 packages | Gawk

Report •

August 8, 2009 at 23:26:51
Yes, both of our examples need input sanitizing.

But here's the equivalent Perl solution of the Python solution.

print "Enter your math equation ";
print eval <STDIN>;

Enter your math equation (5+3) * 7 / 4.454 * 36

Report •

August 9, 2009 at 04:28:17
Python's eval can be passed parameters to minimise such things, though IMO, it may not be the most thorough, however it is at least something. See here

Perl's eval() is much more "dangerous" (for lack of better word)

GNU win32 packages | Gawk

Report •

August 9, 2009 at 08:59:19
I wouldn't use the word "dangerous", but it might need more error handling code than Python's eval, only a person that knows each language equally well can say for sure.

However, I wouldn't use something this simplistic in production code, and I doubt that you would either. I'd start by searching cpan for related modules that have already worked out the some of the problems.

Report •

Ask Question