批量数字错误 [英] numeric error in batch

查看:138
本文介绍了批量数字错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code:

@echo off

setlocal enabledelayedexpansion

set HOUR=00
set MINS=10

if %HOUR% EQU 00 (
set HOUR=12
set /A minDiff=60-!MINS!&set expectedTime=1^.!minDiff!
)

echo expectedTime=!expectedTime!

预期的输出是可变expectedTime的值必须是1.50,但它仅输出1。给出的错误是:
无效number.Numeric常数是任一十进制(17),十六进制(为0x11),或八进制(021)。
请帮忙!!
谢谢

The expected output that is the value of variable expectedTime must be 1.50 , but it outputs 1. only. The error given is: Invalid number.Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021). Please help!! thanks

推荐答案

在code你贴优秀作品对我来说。我怀疑你稍有不同的条件下得到了你的错误比你贴什么。

The code you posted works fine for me. I suspect you got your error under slightly different conditions than what you posted.

SET / A在任意数量的具有领先的0被视为八进制一个非常不幸的功能。因此,08或09的值不是SET / A的有效号码,会给你列举的错误消息。为了得到正确的值,你必须重新present的敏思为8或9(没有前导0)。需要注意的是00至07做工精细,因为这两个八进制和十进制间pretation产生相同的值。

SET /A has a very unfortunate feature in that any number that has a leading 0 is treated as octal notation. So a value of 08 or 09 is not a valid number for SET /A, and will give the error message that you cited. To get the correct value you must represent the MINS as 8 or 9 (no leading 0). Note that 00 through 07 work fine because both octal and decimal interpretation yield the same value.

这是通常用来对付潜在的不必要的前导零的一个技巧是preFIX数字字符串以适当大功耗的10,然后用MOD(余)运算符来提取所需的值。例如,如果你从来没有期望的值大于9999时,那么你可以使用下面的安全与变量val

One trick that is often used to deal with potential unwanted leading zeros is to prefix the numeric string with a suitably large power of 10 and then use the mod (remainder) operator to extract the desired value. For example, if you never expect a value larger than 9999, then you could use the following to safely deal with potential leading zeros in variable VAL

set /a "VAL=10000%VAL% %% 10000"

关于上述技术的好处是它可以结合到一个更大的数学计算,而无需单独之前计算剥离前导零。

The nice thing about the above technique is it can be incorporated into a larger mathematical computation without having to seperately strip the leading zeros prior to the computation.

这篇关于批量数字错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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