Java中由於數據太大自動轉換成科學計數法解決方式


1.java后台

(1)使用BigDecimal類

方式一:String str=new BigDecimal(num+"").toString();
方式二:String str=new BigDecimal(num.toString()).toString();

(2)使用DecimalFormat類
//注意,這種方式是保留幾位小數
String str=new DecimalFormat("0.00").format(num);


2.JSP頁面

(1)使用jstl標簽fmt:formatNumber
導入:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
使用:<fmt:formatNumber value="num " pattern="#.##" minFractionDigits="2" > </fmt:formatNumber> 

(2)使用js腳本
var str=parseFloat(num).toString();

 

* num : 科學計數法表示的數據

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
  <title>JSTL fmt:formatNumber 標簽</title>
</head>
<body>
<h3>數字格式化:</h3>
<c:set var="balance" value="120000.2309" />
<p>格式化數字 (1): <fmt:formatNumber value="${balance}" 
            type="currency"/></p>
<p>格式化數字 (2): <fmt:formatNumber type="number" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化數字 (3): <fmt:formatNumber type="number" 
            maxFractionDigits="3" value="${balance}" /></p>
<p>格式化數字 (4): <fmt:formatNumber type="number" 
            groupingUsed="false" value="${balance}" /></p>
<p>格式化數字 (5): <fmt:formatNumber type="percent" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化數字 (6): <fmt:formatNumber type="percent" 
            minFractionDigits="10" value="${balance}" /></p>
<p>格式化數字 (7): <fmt:formatNumber type="percent" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化數字 (8): <fmt:formatNumber type="number" 
            pattern="###.###E0" value="${balance}" /></p>
<p>美元 :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></p>
</body>
</html>

運行結果如下:

數字格式化:

格式化數字 (1): ¥120,000.23

格式化數字 (2): 000.231

格式化數字 (3): 120,000.231

格式化數字 (4): 120000.231

格式化數字 (5): 023%

格式化數字 (6): 12,000,023.0900000000%

格式化數字 (7): 023%

格式化數字 (8): 120E3

美元 : $120,000.23

 

jstl標簽fmt:formatNumber的使用詳情請見http://www.runoob.com/jsp/jstl-format-formatnumber-tag.html


免責聲明!

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



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