導航:首頁 > 編程語言 > java數字補0

java數字補0

發布時間:2023-03-07 09:59:36

java 如何補零

這樣是一個例子

❷ java一個數字的位數不夠怎麼在前面加0

具體操作如下:

String str1="1";

DecimalFormat df=new DecimalFormat("0000");

String str2=df.format(Integer.parseInt(str1));

System.out.println(str2);

❸ JAVA溫度補0問題

你這個需求比較特殊,像1.2 -> 01.2,01.2已經不是正常的數字了(正常數字整數部分左側不能有零),拿只能當字元串來處理了。代碼如下:

public class Test {

public static void main(String[] args) {
handle("1.2");
handle("-1.23");
handle("-12.1");
handle("-1.2");
handle("11");
}

private static void handle(String temperature) {
String[] temp = temperature.split("\.");
if (temp.length == 1) {//無小數點
//整數直接在前面補零
temp[0] = String.format("%03d", Integer.valueOf(temp[0]));
System.out.println(temperature + " -> " + temp[0]);
} else if (temp.length == 2) {//有小數點
if (temp[0].startsWith("-")) {//是負數
temp[0] = temp[0].substring(1, temp[0].length());//先去掉負號
if (temp[0].length() + temp[1].length() < 3) {//當整數部分長度和小數部分長度相加不足三位時,如1.2,則整數部分補(3-小數部分位數)個零
temp[0] = String.format("%0" + (3 - temp[1].length()) + "d", Integer.valueOf(temp[0]));
}
System.out.println(temperature + " -> " + "-" + temp[0] + "." + temp[1]);
} else {//是正數
if (temp[0].length() + temp[1].length() < 3) {//當整數部分長度和小數部分長度相加不足三位時,如1.2,則整數部分補(3-小數部分位數)個零
temp[0] = String.format("%0" + (3 - temp[1].length()) + "d", Integer.valueOf(temp[0]));
}
System.out.println(temperature + " -> " + temp[0] + "." + temp[1]);
}
}
}
}

閱讀全文

與java數字補0相關的資料

熱點內容
怎麼看其他電腦共享文件夾 瀏覽:506
py文件夾後綴 瀏覽:716
你對我們的app有什麼建議 瀏覽:577
phpgetcookie 瀏覽:138
程序員最煩遇到的單詞 瀏覽:123
開始伺服器升級需要什麼 瀏覽:980
gcc中的編譯選項 瀏覽:188
程序員長沙開滴滴 瀏覽:138
十幾加幾的進位加法演算法 瀏覽:384
c語言實現字母加密成字母 瀏覽:328
linux重啟java服務 瀏覽:53
ubuntu的命令行在哪裡 瀏覽:981
伺服器tk是什麼意思 瀏覽:397
防止軟體加密碼卸載 瀏覽:182
自建伺服器與雲伺服器 瀏覽:542
已解壓車能過戶能買嗎 瀏覽:775
上網行為linux 瀏覽:353
解壓對身體的好處 瀏覽:72
php服務端框架 瀏覽:455
gitlabpdf 瀏覽:612