在批处理文件中将数字转换为不同的数字

Convert number to different number in batch file

这一定是非常基础的,但我似乎找不到完成它的方法

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

@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 似乎不再是我最好的朋友,我们似乎无法就此进行交流。

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

想出了一种使用选择输入的方法,如果有人有更简洁或不同的方法来做到这一点,请告诉我...还有 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

您可以在循环中使用 choice 来创建基于 'slider' 的键,然后使用 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 系统,将最后设置的卷存储在备用数据流中并重新分配 return 上的最后一个值的变体:

@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

注意:使用此输入法,不能输入无效输入。

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

第一步是在输入前加上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