Why is YAML.load returning the wrong numeric value?

☆樱花仙子☆ 提交于 2019-12-10 21:51:50

问题


Why is YAML.load returning the wrong value?

ruby-1.9.2-p0 :006 > a = YAML.load('merchant_id: 014213245611111')
 => {"merchant_id"=>843333440073} 
ruby-1.9.2-p0 :007 > a["merchant_id"]
 => 843333440073 

I'm on ruby 1.9.2-p0, rvm, ubuntu10.10, 64bit.


回答1:


The YAML parser is treating "014213245611111" as an octal (base-8) number, rather than a string. Wrap it in quotes to preserve the leading 0.




回答2:


A leading 0 signifies an octal number — 14213245611111 octal == 843333440073 decimal. If you need to have leading zeroes, you should use a string value instead of numeric.



来源:https://stackoverflow.com/questions/5229919/why-is-yaml-load-returning-the-wrong-numeric-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!