# 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
 hi,i made a calculator in a batch file using this code:@echo offtitle My Calculatorcls:aecho.set /p math=Whatchoo gonna calculate? set /a math2=%math%echo %math% = %math2%goto ahowever, i soon discovered it didnt show decimals. is there any way to do this??

See More: batch file calculator with decimals

#1
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 •

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

Report •

#3
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

#4
August 4, 2009 at 16:10:14
 Hi,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) | www.hotclik.net@echo offsetlocal enabledelayedexpansion:inicclsecho Calculadora.BATecho ----------------------------------------------------------echo * The arithmetic expression must not contain spaces.echo * Use dot to separate integers/decimals (no commas).echo * Arithm operators: +-*/ Example: 123.55*868.33echo ----------------------------------------------------------echo.echo To Quit: X + ENTERset /p "expr=Type the expression: " set r1=!expr:~0,1!if %r1%==x goto fimif %r1%==X goto fim:Tamanho da expressaoset exprX=%expr%x:TamanhoLoopif not "!exprX:~%TamanhoExpr%,1!"=="" ( set /a TamanhoExpr += 1 goto TamanhoLoop)set /a TamanhoExpr=%TamanhoExpr%-1set /a cont=-1set sinal=indefset def=naoset op1=xxxset PosicaoSinal=0:calcset /a cont=%cont%+1if %cont% == %tam% goto proxset dig=!expr:~%cont%,1!if %dig%==+ if %sinal%==indef (set sinal=adic goto prox)if %dig%==- if %sinal%==indef (set sinal=subtrset 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 proxgoto calcgoto prox:proxset /a ps=%cont%+1set /a pss=%ps%-1set /a tam1=%cont%set /a tam2=%TamanhoExpr%-%tam1%-1set op1=!expr:~0,%cont%!set op2=!expr:~%ps%,%tam2%!set opx=!expr:~%pss%,1!set /a p1=0set /a cpt=0:remov1::removendo ponto decimalset /a cpt=%cpt%+1if %cpt%==%tam1% goto prox2set sub1=!op1:~%cpt%,1!if %sub1%==. goto prox2goto remov1:prox2set /a p1=%cpt%+1set /a p2=0set /a cpt=0:remov2set /a cpt=%cpt%+1if %cpt%==%tam2% goto prox3set sub2=!op2:~%cpt%,1!if %sub2%==. goto prox3goto remov2:prox3set /a p2=%cpt%+1set string1=%op1%00000set string2=%op2%00000set /a casas1=2set /a casas2=2if %sinal%==mult (set /a casas1=1set /a casas2=1)if %sinal%==div (set /a casas1=1set /a casas2=1)if p1==%tam1% goto prox4set string1=!string1:.=!:prox4if p2==%tam2% goto x1000set string2=!string2:.=!:x1000set /a str1=%p1%+%casas1%set string1=!string1:~0,%str1%!set /a str2=%p2%+%casas2%set string2=!string2:~0,%str2%!set /a virg1=1set /a virg2=1::prevenindo No.Octal (0500 p.ex):octal1set oc=!string1:~0,1!if %oc%==0 (set string1=!string1:~1,15!set /a virg1=%virg1%+1goto octal1) else goto octal2:octal2set oc=!string2:~0,1!if %oc%==0 (set string2=!string2:~1,15!set /a virg2=%virg2%+1goto octal2) else goto prox5:prox5set /a val1=%string1%set /a val2=%string2%if %sinal%==adic goto adicif %sinal%==subtr goto subtrif %sinal%==mult goto multif %sinal%==div goto div:adicset /a result=%val1%+%val2%goto ponto:subtrset /a result=%val1%-%val2%goto ponto:multset /a y1=%val1%/100set /a y2=%val2%/100set /a prov1=%y1%*%y2%set /a w1=%y1%*100set /a w2=%y2%*100set /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=0if not %prov2%==0 set rest=!prov2:~1,3!if not %prov2%==0 set /a rest=%rest%*10)set /a prov2=%prov2%/10000set /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:divset oc=%val1%00set /a val1=%oc%set /a result=%val1%/%val2%if %result% gtr 9 goto pontoset fu=0.0if %result%==0 set fu=0.00set /a val1=%val1%*100set /a result=%val1%/%val2%set resultado=%fu%%result%goto resul:pontoset result=%result%:virgLoopif not "!result:~%TamanhoRes%,1!"=="" ( set /a TamanhoRes += 1 goto virgLoop)set /a cpt=%TamanhoRes%-3if %sinal%==div set /a cpt=%cpt%+1set /a oc=%cpt%set resultado=!result:~0,%cpt%!.!result:~%oc%,3!:resulset fu=!resultado:~0,1!if %fu%==. set resultado=0%resultado%set /a result=resultadoif %result% gtr 2146999999 set resultado=%resultado%... ERROR: too large numeral...echo. echo Result: %resultado%echo.echo.set /p "oc= Press any key...":releset 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:fim

Report •

#5
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 •

#6
August 8, 2009 at 12:35:12
 @ricardo647wow. thats an epic code.thanks

Report •

#7
August 8, 2009 at 14:02:30
 Here's a perl solution.```#!/usr/bin/perl use strict; use warnings; print "Whatchoo gonna calculate? "; chomp(my \$calc = ); my \$answer = eval \$calc; print \$answer, \$/; ```

Report •

#8
August 8, 2009 at 19:43:41
 @Fish>>Here's a perl solution.though its workable, however, still need input sanitizing(eg, check for numbers...etc)```# ./perl.pl 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 •

#9
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.C:\test>type calc.plprint "Enter your math equation ";print eval ;C:\test>calc.plEnter your math equation (5+3) * 7 / 4.454 * 36452.626852267625

Report •

#10
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 herePerl's eval() is much more "dangerous" (for lack of better word) GNU win32 packages | Gawk

Report •

#11
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 •