int k = 17; System.out.println(Integer.toBinaryString(k)); //转二进制 System.out.println(Integer.toOctalString(k)); //转八进制 System.out.println(Integer.toHexString(k)); //转十六进制
int r =2; System.out.println(Integer.toString(k,r)); //转r进制 r=8; System.out.println(Integer.toString(k,r)); //转r进制 r=16; System.out.println(Integer.toString(k,r)); //转r进制
double a = 123.456789; double b = 123.444444; String sa = String.format("%.2f",a); System.out.println(sa); String sb = String.format("%.2f",b); System.out.println(sb);
//输出 123.46 123.44
//简写 System.out.println(String.format("%.2f",a));
2.2 DecimalFormat的format方法
1 2 3 4 5 6 7 8 9 10 11 12 13
double a = 123.456789; double b = 123.444444; DecimalFormat dfa = new DecimalFormat("0.00"); System.out.println(dfa.format(a)); DecimalFormat dfb = new DecimalFormat("0.00"); System.out.println(dfb.format(b));
/** * @Author DragonOne * @Date 2021/12/5 21:27 * @墨水记忆 www.tothefor.com */ publicclassMain{ publicstatic BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); publicstatic BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); publicstatic StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); publicstatic PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
publicstaticvoidmain(String[] args)throws Exception { Integer[] a = {12, 56, 4, 56, 0, 1, 3, 2}; Integer[] b = {12, 56, 4, 56, 0, 1, 3, 2}; int n = a.length;
Arrays.sort(a, 0, n, new cmp2()); //升序 for (int i = 0; i < n; ++i) { System.out.println(a[i]); } System.out.println("========================"); Arrays.sort(b, 0, n, new cmp()); //降序 for (int i = 0; i < n; ++i) { System.out.println(b[i]); } closeAll(); }