Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼
更新時(shí)間:2016年09月18日 14:42:53 投稿:mrr
這篇文章主要介紹了Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
廢話不多說(shuō)了,關(guān)鍵代碼如下所示:
package cn.com.songjy;
import java.text.NumberFormat;
//Java 中給數(shù)字左邊補(bǔ)0
public class NumberFormatTest {
public static void main(String[] args) {
// 待測(cè)試數(shù)據(jù)
int i = 1;
// 得到一個(gè)NumberFormat的實(shí)例
NumberFormat nf = NumberFormat.getInstance();
// 設(shè)置是否使用分組
nf.setGroupingUsed(false);
// 設(shè)置最大整數(shù)位數(shù)
nf.setMaximumIntegerDigits(4);
// 設(shè)置最小整數(shù)位數(shù)
nf.setMinimumIntegerDigits(4);
// 輸出測(cè)試語(yǔ)句
System.out.println(nf.format(i));
}
}
/**
* Java里數(shù)字轉(zhuǎn)字符串前面自動(dòng)補(bǔ)0的實(shí)現(xiàn)。
*
*/
public class TestStringFormat {
public static void main(String[] args) {
int youNumber = 1;
// 0 代表前面補(bǔ)充0
// 4 代表長(zhǎng)度為4
// d 代表參數(shù)為正數(shù)型
String str = String.format("%04d", youNumber);
System.out.println(str); // 0001
}
}
//流水號(hào)加1后返回,流水號(hào)長(zhǎng)度為4
private static final String STR_FORMAT = "0000";
public static String haoAddOne_2(String liuShuiHao){
Integer intHao = Integer.parseInt(liuShuiHao);
intHao++;
DecimalFormat df = new DecimalFormat(STR_FORMAT);
return df.format(intHao);
}
好了,以上代碼就是關(guān)于Java String字符串補(bǔ)0或空格的實(shí)現(xiàn)代碼,非常不錯(cuò),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
java實(shí)現(xiàn)區(qū)域內(nèi)屏幕截圖示例
這篇文章主要介紹了java截圖示例,需要的朋友可以參考下2014-04-04
SpringBoot整合Redis的哨兵模式的實(shí)現(xiàn)
Redis提供了哨兵模式來(lái)處理主從切換和故障轉(zhuǎn)移,本文主要介紹了SpringBoot整合Redis的哨兵模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
SpringSecurity自定義AuthenticationProvider無(wú)法@Autowire的解決
這篇文章主要介紹了SpringSecurity自定義AuthenticationProvider無(wú)法@Autowire的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

