在公司實習時候發現個問題,就是大的浮點數從數據庫取出后變成了科學計數法顯示,而原有的驗證控件并不能識別科學技術法,造成數據無法正常保存,臨時找到了個解決辦法。 當輸入大數據的時候浮點類型在從數據庫取出的時候會以科學計數法的形式顯示。 比如輸
在公司實習時候發現個問題,就是大的浮點數從數據庫取出后變成了科學計數法顯示,而原有的驗證控件并不能識別科學技術法,造成數據無法正常保存,臨時找到了個解決辦法。
當輸入大數據的時候浮點類型在從數據庫取出的時候會以科學計數法的形式顯示。
比如輸入:2222222222 回顯時頁面顯示為:2.222222222E9 這樣在修改時候無法正常保存。
解決辦法:
1.方法一
例如車輛單價:
注意黑體字部分是車輛單價的顯示方式,maxIntegerDigits為整數部分顯示的最大長度,maxFractionDigits為小數部分顯示的最大長度。
這樣可以將2.222222222E9轉化成2,222,222,222 之后采用字符串匹配方式去掉” , ”,采用正則表達式處理,函數為
2.方法二
用正則表達式處理字符串,去掉格式化之后的浮點數類型
function formatNum(id){
document.getElementById(id).value=document.getElementById(id).value.replace(/,/gi,'');
}
這個函數可以將2,222,222,222中的” , ”去掉,使其正常顯示。
其中id為輸入框的id。
在
的onload屬性中添加如下語句調用formatNum("cldj");
3.方法三
代碼如下
<%
java.text.DecimalFormat df=new java.text.DecimalFormat("#0.00000");//指定轉換的格式
Object cash=request.getAttribute("cash");
if("".equals(cash)||cash==null){cash="0";}
String str=df.format(cash);//將double類型的值轉換為String類型
%>
<%=str %>
4.方法四
import java.text.DecimalFormat;
public class tetr
{
public static String padDoubleLeft(Double d, int totalDigit,int fractionalDigit) {
String str="";
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setMinimumFractionDigits(fractionalDigit);
decimalFormat.setMaximumFractionDigits(fractionalDigit);
decimalFormat.setGroupingUsed(false);
decimalFormat.setMaximumIntegerDigits(totalDigit - fractionalDigit - 1);
decimalFormat.setMinimumIntegerDigits(totalDigit - fractionalDigit - 1);
str=decimalFormat.format(d);
/**
* 去掉前面的0(比如000123213,最后輸出123213)
*/
while(str.startsWith("0"))
{
str=str.substring(1);
}
return str;
}
public static void main(String[] args)
{
String str="";
Double d=1.7949E+7;
/**d表示你要轉化的數字*/
/**50表示總共要留多少位數,
* 2表示小數位數,
* 如果不知道總共留多少位,可以給大一些(比如此處為50)
* 一般情況下,總位數不會超過50,除非客戶有這個需要
* 小數按照客戶要求來作
* */
str=padDoubleLeft(d,50, 2);
System.out.println(str);
}
}
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com