Java Android 開發數字不足位數前面補0


import java.text.DecimalFormat;
public void changeColor(View view) {
        DecimalFormat decimalFormat = new DecimalFormat("000");
        //獲取隨機數對象,產生三個隨機數值(RGB值)
        Random x = new Random();

        int red = x.nextInt(256);
        String sred = decimalFormat.format(red);
        txvR.setText("紅:"+sred);
        txvR.setTextColor(Color.rgb(red,0,0));

        int green = x.nextInt(256);
        String sgreen = decimalFormat.format(green);
        txvG.setText("綠:"+sgreen);
        txvG.setTextColor(Color.rgb(0,green,0));

        int blue = x.nextInt(256);
        String sblue = decimalFormat.format(blue);
        txvB.setText("藍:"+sblue);
        txvB.setTextColor(Color.rgb(0,0,blue));

        //設置界面最上方的按鈕 button 的文字顏色
        button.setTextColor(Color.rgb(red,green,blue));

        //設置界面最下方的空白 LinearLayout 的背景顏色
        colorBlock.setBackgroundColor(Color.rgb(red,green,blue));
    }
 import java.text.DecimalFormat;

//如果數字1是字符串,如下處理:
String string="1";
DecimalFormat decimalFormat =new DecimalFormat("0000");
String string2=decimalFormat.format(Integer.parseInt(str1));
System.out.println(string2);

//如果數字1是整型,如下處理:
int string=1;
DecimalFormat decimalFormat =new DecimalFormat("0000");
String string2=decimalFormat.format(string);
System.out.println(string2);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM