導航:首頁 > 編程語言 > javastring補零

javastring補零

發布時間:2023-03-19 10:33:55

java數字自動補零

public class P {
public static void main(String[] args){
String pattern="000";
java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
int i = 10,j=6;
System.out.println("i="+df.format(i)+"\nj="+df.format(j));
}
}
---------------------輸出-----------------------
i=010
j=006

❷ java 如何補零

這樣是一個例子

❸ 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

具體操作如下:

String str1="1";

DecimalFormatdf=new DecimalFormat("0000");

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

System.out.println(str2);

JAVA

❺ JAVA中如何在基本數字類型中開頭輸入0

不行,你只能轉成字元串,然後在前面補零,可以直接使用String.format(),包括向左補零、向右補零、向左補空格、向右補空格、保留多少位小數這些都可以用這個方法,你查一下資料。

❻ java 0開頭2位字元串,加1補零,怎麼寫

x =Integer.parse(x);

if((x+1)<10){x+=1;x='0'+x;}
先把字元串轉為數字,再把數字轉為字元串,簡單暴力。

❼ java的字元型數組補零

import java.util.Scanner;

public class T
{
public static void main(String[] args)
{
int n;
System.out.print("請輸入數組a的長專度屬:");
Scanner sc = new Scanner(System.in);
n=sc.nextInt();

char[] a = new char[n];
char[] b = new char[200];
for(int i=0;i<n;i++)
a[i]='1';
for (int i = 0; i < 200; i++)
b[i]='0';
for(int j=0;j<n;j++)
b[199-j]=a[j];
System.out.println(b);
}
}

❽ 在Java中怎麼把1到9999的數字轉成 4位字元串,左邊補0 如 0001 0002 0003

publicstaticvoidmain(String[]args){

//在Java中怎麼把1到9999的數字轉成4位字元串,左邊補0如000100020003

System.out.println("請輸入版一個1-9999之間的數字:權");

Scanners=newScanner(System.in);

Stringstr=s.next();

char[]ary1=str.toCharArray();

char[]ary2={'0','0','0','0'};

System.array(ary1,0,ary2,ary2.length-ary1.length,ary1.length);

Stringresult=newString(ary2);

System.out.println(result);

}

---請輸入一個1-9999之間的數字:

3

0003

閱讀全文

與javastring補零相關的資料

熱點內容
手機存儲應用程序 瀏覽:284
頁面自適應屏幕如何調整代碼數據 瀏覽:681
jsjson工具 瀏覽:299
資料庫中如何備份一張表的數據 瀏覽:739
網路設備能用到什麼 瀏覽:64
暴風轉碼如何添加文件夾 瀏覽:515
延安整合網路營銷有哪些 瀏覽:74
查找word打開過的文件在哪裡 瀏覽:137
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65
word怎麼插入羅馬數字i 瀏覽:315
哪個網站可以找到法人代表 瀏覽:106

友情鏈接