2014-09-24 21 views
1

我使用Windows 7,我有以下幾點:如何在批處理文件中顯示餘數?

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

所以任何他們鍵入的數量得到由2分但如果它是一個奇數,它只是幾輪下來的整數。例如,如果我輸入7它會給我6,因爲7/2是3.5對嗎?但它只是將整數舍入到整數,而不是留下小數或在這種情況下爲3.5。有沒有辦法讓它顯示出來?由於

+0

[批處理中的浮點除法]可能的重複(http://stackoverflow.com/questions/1503888/floating-point-division-in-a-dos-batch) – ne1410s 2014-09-24 20:19:23

+0

我已經看到了這一點,並做了不理解它。 – GregRogers 2014-09-24 20:22:49

回答

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

編輯:

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% 

但這隻會由2

部門對於真正的浮點運算,你可以通過命令行和JScript函數eval使用MSHTA

@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% 
+0

啊,非常感謝。 – GregRogers 2014-09-24 20:24:03

+0

@GregRogers如果這種方法是可以的,你可以接受的答案,所以我們都會得到一些點:) – npocmaka 2014-09-24 20:25:12

+0

我會,但我必須等待,因爲它不會讓我在短時間內。 – GregRogers 2014-09-24 20:26:34

0

沒有辦法使它正常工作,因爲技術上不支持數學與浮游物。

您可能想使用powershell來做到這一點。這會容易很多。

+0

這正是我所尋找的。我只需要一些東西來承認它「是」一個小數,而不是實際的小數點:) – GregRogers 2014-09-24 20:27:21

相關問題