Java String字串補0或空格

來源:互聯網
上載者:User

標籤:bsp   str   instance   ++   整數   done   []   com   get   

原文:http://www.open-open.com/code/view/1471488086408

 

package cn.com.songjy;import java.text.NumberFormat;//Java 中給數字左邊補0public class NumberFormatTest {    public static void main(String[] args) {        // 待測試資料        int i = 1;        // 得到一個NumberFormat的執行個體        NumberFormat nf = NumberFormat.getInstance();        // 設定是否使用分組        nf.setGroupingUsed(false);        // 設定最大整數位元        nf.setMaximumIntegerDigits(4);        // 設定最小整數位元        nf.setMinimumIntegerDigits(4);        // 輸出測試語句        System.out.println(nf.format(i));    }}/**   * Java裡數字轉字串前面自動補0的實現。   *     */  public class TestStringFormat {     public static void main(String[] args) {       int youNumber = 1;       // 0 代表前面補充0       // 4 代表長度為4       // d 代表參數為正數型       String str = String.format("%04d", youNumber);       System.out.println(str); // 0001     }   }  //流水號加1後返回,流水號長度為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);    }

 

Java String字串補0或空格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.