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