1

I am using Windows 7 and I have the following:

set /p param=Please type a number
set /a answer= %param%/2
echo Your answer is %answer%

So anything they type in as a number gets divided by 2. However if it is an odd number, it just rounds it down to the whole number. For example if I typed in 7 it will give me 6 because 7/2 is 3.5 right? But it just rounds it down to the whole number instead of leaving a decimal or in this case a 3.5. Is there a way I can make it display it? Thanks

GregRogers
  • 13
  • 1
  • 6

2 Answers2

0
set /p param=Please type a number
set /a answer= %param%/2
set /a mod=%param%%%2
echo Your answer is %answer%.%mod%

EDIT:

set /p param=Please type a number
set /a answer= %param%/2
set /a mod=%param%%%2
if %mod% equ 1 set mod=5
echo Your answer is %answer%.%mod%

but this will only with division by 2.

For real float point operations you can use MSHTA through command line and jscript function eval:

@echo off
setlocal

set /p exp=expression to calculate: 


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


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

echo result=%result%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

There is no way of making that work properly, since technically does doesn't support math with floats.

You might want to use powershell for this. It will be a lot easier.

Arun
  • 941
  • 6
  • 13
  • It was just what I was looking for. I just needed something to acknowledge that it "was" a decimal not the actual decimal :) – GregRogers Sep 24 '14 at 20:27