当我尝试执行以下操作时(01434.210 instead of 1434.210)
$val=22749.220-(21315.010+01434.210)
print $val
Run Code Online (Sandbox Code Playgroud)
我得到这些输出
output 638.207900000001
Run Code Online (Sandbox Code Playgroud)
但根据我的输出必须0.
我错过了什么?
bri*_*foy 13
字面数字中的前导0使Perl解释我的基数为8的值:
123 # 123, in decimal
0123 # 123 in octal, but 83 in decimal
Run Code Online (Sandbox Code Playgroud)
对于转换为数字的字符串,这是不一样的.在那些Perl中忽略了领先的0.字符串到数字的转换仅在base-10中处理:
"123" + 0 # 123
"0123" + 0 # still 123
Run Code Online (Sandbox Code Playgroud)
在注释的示例中,将文字编号转换为前导零的字符串.当您将该字符串转换回其数字形式时,您将获得与您开始时相同的值:
$val=sprintf("%05d",1434); # converting 1434 to the string "01434"
print $val; print "\n"; # still a string
print $val+21315; # "01434" + 21315 => 1434 + 21315
print "\n";
print 01434+21315; # oct(1434) + 21315 => 796 + 21315
Run Code Online (Sandbox Code Playgroud)
前导零表示法有助于某些通常使用八进制数的内置函数,例如那些处理unix权限的内置函数:
chmod 0644, @files
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |