【问题标题】:Convert number to different number in batch file在批处理文件中将数字转换为不同的数字
【发布时间】:2021-06-03 20:43:54
【问题描述】:

这一定是非常基本的,但我似乎找不到完成这项工作的方法

我的批处理脚本是这样的:

@echo off
echo Type In Desired Volume And Press Enter
echo.
echo 0 = 0 %%
echo 1 = 100 %%
echo 0.10 = 10 %%
echo 0.65 = 65 %%
echo.
set /p input=
echo %input% > "C:\SetVol\Source\Volume.txt

我想让用户输入一个介于0100 之间的数字,而不是0.10,例如用于10% 的音量,以使其更加用户友好。但如果用户输入10,我仍然需要将0.10 输出到文本文件。

Google 似乎不再是我最好的朋友,我们似乎无法就此进行交流。

如果有人可以帮助我入门,那就太好了。

【问题讨论】:

  • 批处理文件不支持浮点数 stackoverflow.com/questions/25951196/… 。考虑改用 powershell
  • 没有使用浮点数@Renat,只是字符串! TommyMaes,您的最终用户应该输入= 之前的字符串还是= 之后的字符串?
  • set /p input= 之后,用户现在将输入 0.10 表示 10% 的音量或 0.25 表示 25% 的音量,依此类推。但我希望他们输入 25 而不是 0.25。 powershell脚本仍然需要0.25,所以我需要将用户输入转换为那个。我真的很想在批处理文件中完成这种转换。
  • 如果你想让他们输入 25 而不是 0.25,那你为什么不告诉他们呢? Set /P "input=Desired Volume [Enter 25 for 25%% or 0.25]>"
  • @Compo 那么我一定无法理解你的贡献。

标签: batch-file input numbers output


【解决方案1】:

一种简单的方法是将输入的数字转换为请求的输出格式。

第一步是在输入前加上0.

set /p input=[Enter volume in %%]: 
set "output=0.%input%"
echo %input% > "C:\SetVol\Source\Volume.txt

但是对于像2% 这样的一位数字值,这将失败,变为0.2 而不是0.02

这可以通过在每个数字前面加上00 来修复,并取最后三位数字并在它们之间添加一个点。

set /p input=[Enter volume in %%]: 
set "temp=00%input%"
set "output=%temp:~-3,1%.%temp:~-2%"
echo %output% > "C:\SetVol\Source\Volume.txt

【讨论】:

  • 哦,这很整洁!效果很好。我对此有点陌生,但我想我理解代码,如果我错了,请纠正我:set "output=%temp:~-3,1%.%temp:~-2%" first %temp:~-3 从 temp 中获取最后 3 个数字,然后 ,1 表示使用你刚刚使用的 3 个数字中的第一个取自温度。然后输入%.% 点,然后temp:~-2% 输入 temp 的最后 2 位数字。
【解决方案2】:

您可以在循环中使用选择来创建基于键的“滑块”,然后使用 if 条件修改变量值以包含 0. 前缀或 1

@Echo off

 set "volume=50"
:Volume
 cls
 Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
 For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
  If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
  If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
  If not "%%G"=="C" Goto :Volume
 )

 IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
  Set "Volume=0.0%Volume%"
 ) Else Set "Volume=0.%Volume%"
:#your script here

对于 NTFS 系统,一种将最后设​​置的卷存储在备用数据流中并在返回时重新分配最后一个值的变体:

@Echo off

 set "volume=50"
 For /f "Usebackq delims=" %%G in ("%~f0:Volume")Do Set "%%G"
:Volume
 cls
 Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
 For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
  If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
  If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
  If not "%%G"=="C" Goto :Volume
 )
 Set Volume >"%~f0:Volume"
 IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
  Set "Volume=0.0%Volume%"
 ) Else Set "Volume=0.%Volume%"
:#your script here

注意:使用这种输入方式,不能输入无效的输入。

【讨论】:

  • 这也很好!现在不会在我的程序中使用它,但我正在玩它以获取未来的知识:) thnx !
  • 最终使用它稍后再次将其转换回来:set /P vol=<c:\setvol\source\volume.txtset temp2=) if %vol% equ 1.00 ( set "temp2=100" ) else if %vol% lss 0.01 ( set "temp2=0" ) else if %vol% LSS 0.10 ( set "temp2=%vol:~-1%" ) else if %vol% gtr 0.10 ( set "temp2=%vol:~-2%" ) else if %vol% equ 0.10 ( set "temp2=10" ) )
【解决方案3】:

想出了一种使用选择输入的方法,如果有人有更简洁或不同的方法,请告诉我...还有 291 行要编辑...

@echo off
echo Choose Audio Volume 0-100 %%
:choice
set /P c=
if /I "%c%" EQU "0" goto :0
if /I "%c%" EQU "1" goto :1
if /I "%c%" EQU "2" goto :2
goto :choice

:0
echo 0 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:1
echo 0.01 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:2
echo 0.02 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2017-12-02
    相关资源
    最近更新 更多