parseInt bug
来源:互联网 发布:淘宝如何做详情页关联 编辑:程序博客网 时间:2024/06/12 05:17
here is a "bug" with the parseInt JavaScript function. The bug is not something that will affect you very often, but it is something you should be aware of. We've seen the bug in every browser except Opera.
We've created a button to demonstrate the bug. The bug is that parseInt can return an incorrect value. For example, parseInt("08") results in 0 instead of 8. And parseInt("09") results in 0 instead of 9. The reason for this is because the zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers. The button below builds statements from parseInt("01") through parseInt("09") and shows what the resulting value is. But it also does parseFloat("01") through parseFloat("09"). This shows that the bug does not exist with parseFloat.
Keep in mind that this bug only happens when the value being checked is a string and only when the string starts with a leading zero. So that's why it is difficult to notice. But if you're dealing with a web page that has user input, there's nothing prevening the user from entering 08 for a number field. To be 100% confident that you won't see the bug, use one of these two techniques:
parseInt(parseFloat(<my text value>))
parseInt(<my text value>, 10)
The "10" in the second example tells the browser that base-10 values should be used. Here is the button so you can see what's happening. The button puts up an alert box with the result of the "buildString" function, which is shown below:
- parseInt bug
- parseInt()
- parseInt()
- parseInt()
- parseInt()
- ParseInt
- parseInt()
- parseInt
- ParseInt
- parseInt()
- parseInt
- parseInt
- 关于javascript中parseInt函数的一个所谓的bug
- 关于javascript中parseInt函数的一个所谓的bug
- js的四舍五入实现和parseInt 函数小bug
- Javascript 函数parseInt()转换丢失精度出现bug
- js的四舍五入实现和parseInt 函数小bug
- js最大时间间隔为一个月的处理以及parseInt(08)和parseInt(09)的一个bug
- 关于HadoopMapReduce的精彩介绍
- hibernate的一点总结
- .NET平台下Web测试工具横向比较
- JMagick 学习四:图片翻转
- Linux下使用core文件调试程序
- parseInt bug
- JMagick 学习五:解决不能读取中文路径问题
- AVR GCC 编译器调用规则
- ChildDadFeed.java(设计模式)
- JMagick 学习六:图片加中文字
- 用#define定义注释的宏
- request对象的一些方法总结,request对象,request
- JMagick 学习七:图片旋转
- 关于数字电视的常识