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

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接