为什么js的number类型超过16位的数字变成0,且第16位会加一

昨天遇到一个问题:通过get请求,将“http://127.0.0.1:8080/ServletWebTest/test/test1.jsp?id=1111111111111111111” id的值直接通过<%=id%>的方式弹出。

页面显示id=1111111111111111111,但alert中显示“1111111111111112000”?

第一:其js将id数据转换位number类型,16位以后的数据变为0

(具体的原因没找到,但是根据科学计数法,只显示小数点后16位,如"2.76325901626201e+21")

网上找到一个帖子,可以大概的看到原因,如下:

地址:https://segmentfault.com/q/1010000006692622

 

 

 以下是测试代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//测试
String id = request.getParameter("id");
System.out.println("id="+id);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>测试</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    测试案例:url=127.0.0.1:8080/ServletWebTest/test1.jsp?id=1111111111111111111
    使用《%=%》方式显示值<br>
    
    <hr/>
    id = <%=id %>
    label-id=<label id="label"><%=id %></label>
    <script type="text/javascript">
    	var i = 1111111111111111111;//19位
    	window.onload=function(){
    		alert("id="+<%=id %>);
    		alert("i = "+ i);
    		var a = parseInt(276325901626200949000);
    		var b = parseInt(0.27632590162620094e+22);
			console.log(a,b);
    	}
    </script>
  </body>
</html>

  

 

posted @ 2018-07-17 10:34  咬咬  阅读(6440)  评论(0编辑  收藏  举报