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

熱點內容
安卓系統藍牙耳機如何用 瀏覽:719
為什麼微信不能給appstore充值 瀏覽:493
程序員的保護動物 瀏覽:272
程序員遇到問題去哪個網站 瀏覽:531
安卓手機空格鍵連續輸入怎麼取消 瀏覽:520
壓縮空氣管道流量計 瀏覽:564
ug編程高級教程 瀏覽:177
什麼叫做伺服器已滿 瀏覽:37
暑假哪有教演算法的 瀏覽:136
密碼學的根基是加密 瀏覽:662
stata方差檢驗命令 瀏覽:337
解壓後文件夾里的內容丟失 瀏覽:715
解壓無敵視頻 瀏覽:690
什麼是伺服器辨認不了 瀏覽:129
java如何調用類方法 瀏覽:483
管理孩子的app叫什麼 瀏覽:546
壓縮活動軌跡 瀏覽:674
6米梁加密筋 瀏覽:79
怎麼學好ps如何學好編程 瀏覽:301
c編譯器廠商 瀏覽:114