為什么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>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM