java里面使用string.format如何实现空格右填充?

Python012

java里面使用string.format如何实现空格右填充?,第1张

java里面使用string.format实现空格右填充代码如下:

package cn.com.songjy

import java.text.NumberFormat

public class NumberFormatTest {

public static void main(String[] args) {

int i = 1

NumberFormat nf = NumberFormat.getInstance()

nf.setGroupingUsed(false)

nf.setMaximumIntegerDigits(4)

nf.setMinimumIntegerDigits(4)

System.out.println(nf.format(i))

}

}

public class TestStringFormat { 

public static void main(String[] args) {

int youNumber = 1

String str = String.format("%04d", youNumber) 

System.out.println(str)// 0001

}

}

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)

}

String title="ABC公司工资报表(2004年12月)"

String separator="-----------------------------------------------------------------------------------------------------------------------"

String t1="部 门 "

String t2="人 数"

String t3="基本工资"

String t4="职位工资"

String t5="职称工资"

String t6="工龄工资"

String t7="应付小计"

String t8="保险"

String t9="房积"

String t10="应扣小计"

String t11="所得税"

String t12="合计"

String name="供应科"

Integer i1=4

Double d1=4800.0

Double d2=800.0

Double d3=1200.0

Double d4=1600.0

Double d5=18800.0

Double d6=800.0

Double d7=1200.0

Double d8=1880.0

Double d9=5880.0

Double d10=12920.0

/**

----------------------------------------------------------------------------------------------------------

部 门 人 数 基本工资职位工资职称工资工龄工资应付小计保险房积应扣小计所得税合计

----------------------------------------------------------------------------------------------------------

供应科 4 4800 800 1200 1600 18800 800 1200 1880 5880 12920

----------------------------------------------------------------------------------------------------------

市场处 6 7200 1200 1800 2400 28200 1200 1800 2820 8820 19380

----------------------------------------------------------------------------------------------------------

销售处 7 8400 1400 2100 2800 32900 1400 2100 3290 10290 22610

----------------------------------------------------------------------------------------------------------

行政处 4 4800 800 1200 1600 18800 800 1200 1880 5880 12920

合计 25 30400.0 5000.0 7500.0 10000.0 117900.0 5000.0 7500.0 11790.0 36790.0 81110.0

----------------------------------------------------------------------------------------------------------

*/

PrintWriter pw=new PrintWriter("d:/out.txt")

pw.printf("\n\n%60s\n", title)

pw.println("\n"+separator+"\n")

pw.printf("%-7s%-7s%-7s%-7s%-7s%-7s%-7s%-7s%-7s%-7s%-7s%-7s",t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12)

pw.println("\n"+separator+"\n")

pw.printf("%-6s%-8d%-10.0f%-10.0f%-10.0f%-10.0f%-10.0f%-10.0f%-10.0f%-10.0f%-10.0f%-10.0f",name,i1,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10)

pw.println("\n"+separator+"\n")

name="合计"

i1=25

d1=30400.0

d2=5000.0

d3=7500.0

d4=10000.0

d5=117900.0

d6=5000.0

d7=7500.0

d8=11790.0

d9=36790.0

d10=81110.0

pw.printf("%-7s%-8d%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f%-10.1f",name,i1,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10)

pw.println("\n"+separator)

pw.close()