windows 在批处理文件中将字符串转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25166704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string to integer in a batch file
提问by Ciprian Vintea
I have a variable read from a file:
我有一个从文件中读取的变量:
%var%=13,145
I want to add this value to another value:
我想将此值添加到另一个值:
set /a %var%=%var%+5
but the result is 13+5
, not 13145+5
.
但结果是13+5
,不是13145+5
。
How can I delete this "," from my string?
如何从我的字符串中删除这个“,”?
回答by konsolebox
Do not add commas.
不要添加逗号。
set var=13145
And also, when assigning a variable, don't place it around %
而且,在分配变量时,不要将其放在 %
set /a var=%var% + 5 (Or simply set /a var += 5)
Test:
测试:
@echo off
set var=13145
set /a var=%var% + 5
echo %var%
Output:
输出:
13150
Update
更新
Another solution is to remove those commas with substitution:
另一种解决方案是用替换删除那些逗号:
set var=13,145
set /a var=%var:,=% + 5