導航:首頁 > 編程語言 > 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相關的資料

熱點內容
為什麼叫我買阿里雲伺服器 瀏覽:470
加密貨幣征稅一覽表 瀏覽:959
llc編譯器 瀏覽:922
數控可編程電阻器 瀏覽:757
培訓app源碼 瀏覽:431
phpcurl啟用 瀏覽:533
ubuntu圖形編程 瀏覽:439
jar包啟動命令 瀏覽:678
java數組一維轉二維 瀏覽:498
office批量轉pdf 瀏覽:183
boss直聘程序員多少薪 瀏覽:631
編程字母代表什麼 瀏覽:952
rainmc伺服器地址 瀏覽:456
電信校園網客戶端認證伺服器地址 瀏覽:448
掌閱怎麼看文件夾 瀏覽:341
在伴伴app裡面怎麼拜師傅 瀏覽:945
編程珠璣筆記 瀏覽:282
結束命令行 瀏覽:270
力學原理pdf 瀏覽:737
宏定義編譯後不變 瀏覽:404