今天来谈一下javascript中的Number类型,在javascript中数字类型并没有分成整数类型(Integer)和浮点数类型(Float)
Number就是传统语言的浮点数类型(Float)
传统语言的浮点数类型分为单精度(Single)和双精度(Double)
经过测试:javascript中的Number为双精度(Double)
alert(0.1000000000000021 == 0.1000000000000022) // false
alert(0.10000000000000021 == 0.10000000000000022) // true
?
双精度精确到15-16位。
如果不注意Number的精度问题,可能会碰到如下的问题:
alert(parseInt(0.0000001));// 1
奇怪吧,输出为1!
值得注意,在javascript中每个对象都具有toString()的方法,即:转换为字符串类型。
parseInt参数为String类型,用例3中传递的是一个Number则会自动转成String再执行方法调用。
我猜想parseInt的声明可能是:
function parseInt(param) {
return parseFloat(/^\d+/.exec(param.toString()));
}
alert(0.0000001); // 1e-7
0.0000001转换成字符串变成科学计数法了-- 1e-7。
科学计数法!用到样式表里也没问题。
<div style="width:1e2px;height:2e2px;background-color:Red;"></div>
那么在实际应用中建议优先使用parseFloat方法。
详细解决方案
js Number门类
热度:528 发布时间:2012-11-15 15:16:14.0
相关解决方案
- Parameter index out of range (五 > number of parameters, which is 4)
- error RC2144: PRIMARY LANGUAGE ID not a number,这个异常根本解决办法
- 关于httpWebRequest请求失败异常:The specified call count is not a number
- DATAGRID中显示 的oracle number(10,4)类型数据,怎么把小数点后面的 0 去掉
- Label1.Text = sdr("number").tostring有关问题,待!
- 有时候加了个文件后有下划虚线点上去说:missing xml comment for publicly visible type or number.
- tmboot的时分报错 libifsql.so不能load , The module has an invalid magic number
- tmboot的时候报错 libifsql.so不能load , The module has an invalid magic number,该怎么解决
- Error Loading file-recording number 6,controlsource alias is not found解决办法
- odbc isql db2: 0509-103 The module has an invalid magic number,该如何处理
- 每日背熟一句英语(9)The number you dailed is busy now, pls try again later
- linux 下配置PHP mysql时,在启动/usr/local/mysql/bin/mysqld_safe &报错Operating system error number 13 in a file operation.解决方案
- Tomcat起动web项目报Bad version number in .class file (unable to load class .)错
- JSU省份赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
- new Number()的部类为什么是object
- weblogic起动失败UnsupportedClassVersionError: Bad version number in .class file
- js 之 Number 工具种
- javascript js format number 数目字格式化
- js Number 对象再有几个处理数值的专用方法
- javascript_NAN_NOT A NUMBER
- java.sql.SQLException: ORA-01483: DATE 或 NUMBER 赋值变量的长度无效 透过webService发送数据被限制大小
- Bad version number in .class file的解决办法
- JavaScript中的undefined,null,NaN(Not A Number)跟typeof区别
- object与 Object , number 与Number 区别是什么?解决办法
- the specified call count is not a number :null 异常
- 求sun java 的 serial number,多谢
- java “number++” 的原子性操作有关问题
- Leetcode: Number of 一 Bits
- absent line number information,该怎么处理
- eclipse中编译和运行使用的JDK和JRE级别的有关问题——关于“Bad version number in .class file”错误的解决办法