删除批处理文件前导零 [英] Remove leading zeros in batch file

查看:229
本文介绍了删除批处理文件前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的申请,我得到有前导零的数字。我想修剪前导零和获得的实际数量。我试着用 /它参考分配的右侧是一个算术前pression一个开关。所以我尝试:

In my application, I get a number having leading zeros. I am trying to trim the leading zeros and get the actual number. I tried using /a switch which considers right side of the assignment as an arithmetic expression. So I tried:

SET /a N = 00027

上面给我的输出 23 这是八进制数的十进制等效值 27 。然后我发现该解决方案在线。

The above gave me the output of 23 which is the decimal equivalent of octal number 27. Then I found this solution online.

SET N = 00027
SET /a N = 1%N%-(11%N%-1%N%)/10

这似乎是工作,是给输出的 27 。有没有更容易修剪前导零在批处理文件的方式?

This seems working and is giving the output 27. Is there much easier way to trim the leading zeros in a batch file?

推荐答案

您找到的方法是非常好的。它支持人数达到99,999,999,是非常快的。

The method you found is very good. It supports numbers up to 99,999,999 and is very fast.

有一个更简单的使用了多达9999它采用了模操作编号工作SET / A方式。该方法不能扩展到更大的数字。

There is a simpler way to use SET /A that works with numbers up to 9999. It uses the modulus operation. The method cannot be extended to larger numbers.

 set n=0027
 set /a n=10000%n% %% 10000

在FOR / F方法戴尔发布的作品有任何大小号码(最多8191位)。但是,它需要更多的只是一些工作来处理零值。

The FOR /F method that Dale posted works with "any" size number (up to 8191 digits). However, it needs just a bit more work to handle zero values.

set n=000000000000000000000000000000000000000000000000000027
for /f "tokens=* delims=0" %%N in ("%n%") do set "n=%%N"
if not defined n set "n=0"

这是可以做到同样的事情单行线,替换 || 的IF语句。如果没有迭代FOR / F语句引发一种无声的错误。

It is possible to do the same thing with a single line, substituting || for the IF statement. The FOR /F statement raises a silent error if there are no iterations.

set n=000000000000000000000000000000000000000000000000000027
(for /f "tokens=* delims=0" %%N in ("%n%") do set "n=%%N")||set "n=0"

这篇关于删除批处理文件前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆