如何格式化打印双打 [英] How to format the printing of doubles

查看:200
本文介绍了如何格式化打印双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个从1-10分割表的结果范围从整数到x.x到x.xx我用十进制格式来保持数字到2位小数,但是抛出表的格式。如何打印数字,以便他们都是x.xx

 例如
1 - > 1.00
0.5 - > 0.50
0.333 ... - > 0.33

DecimalFormat df = new DecimalFormat(#。##);
System.out.println(1 2 3 4 5 6 7 8 9 10);
for(double i = 1; i <= 10; i ++){
System.out.print(df.format(i)+);
if(i <10)System.out.print();
else System.out.print();
for(double j = 1; j <= 10; j ++){
double div =(j / i);
System.out.print(df.format(div));
if(div <10)System.out.print();
}
System.out.println();

打印
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
3 0.33 0.67 1 1.33 1.67 2 2.33 2.67 3 3.33
4 0.25 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5
5 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
6 0.17 0.33 0.5 0.67 0.83 1 1.17 1.33 1.5 1.67
7 0.14 0.29 0.43 0.57 0.71 0.86 1 1.14 1.29 1.43
8 0.12 0.25 0.38 0.5 0.62 0.75 0.88 1 1.12 1.25
9 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1 1.11
10 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

i希望将所有内容以x.xx或整齐的方式列印出来
1 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.0
2 0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00 4.50 5.00
3 0.33 0.67 1.00 1.33 1.6 7 2.00 2.33 2.67 3.00 3.33
etc


解决方案

很多方法,但一个是使用 String.format(%。2f,myDouble)另一种方法是使用DecimalFormat,如 DecimalFormat格式= new DecimalFormat(#0.00)然后像 format.format(myDouble)

使用它

要格式化一个表格,可以使用 String.format(...)或者其中一个变种printf。



$ p

pre $ $ $ $ $ $ $ $ $私有静态最终int COLS = 10;
private static final int ROWS = 10;

public static void main(String [] args){
System.out.printf(%2s,);
for(int i = 0; i< COLS; i ++){
System.out.printf(%8d,(i + 1));
}
System.out.println();
for(int i = 0; i int row = i + 1;
System.out.printf(%2d,row);
for(int j = 0; j double myDouble =(double)row /(j + 1);
System.out.printf(%8.2f,myDouble);
}
System.out.println();


$ b $ / code $ / pre

打印出来

  1 2 3 4 5 6 7 8 9 10 
1 1.00 0.50 0.33 0.25 0.20 0.17 0.14 0.13 0.11 0.10
2 2.00 1.00 0.67 0.50 0.40 0.33 0.29 0.25 0.22 0.20
3 3.00 1.50 1.00 0.75 0.60 0.50 0.43 0.38 0.33 0.30
4 4.00 2.00 1.33 1.00 0.80 0.67 0.57 0.50 0.44 0.40
5 5.00 2.50 1.67 1.25 1.00 0.83 0.71 0.63 0.56 0.50
6 6.00 3.00 2.00 1.50 1.20 1.00 0.86 0.75 0.67 0.60
7 7.00 3.50 2.33 1.75 1.40 1.17 1.00 0.88 0.78 0.70
8 8.00 4.00 2.67 2.00 1.60 1.33 1.14 1.00 0.89 0.80
9 9.00 4.50 3.00 2.25 1.80 1.50 1.29 1.13 1.00 0.90
10 10.00 5.00 3.33 2.50 2.00 1.67 1.43 1.25 1.11 1.00


Im making a division table from 1-10 an the results range from integers to x.x to x.xx i used decimalformat to keep the the numbers to 2 decimal places but that throws off the format of the table. How would i print the numbers so that they would all be x.xx

for example
1 -> 1.00
0.5 -> 0.50
0.333... -> 0.33

DecimalFormat df = new DecimalFormat("#.##");
System.out.println("     1       2       3       4       5       6       7       8       9       10");
for(double i=1;i<=10;i++){
  System.out.print(df.format(i)+"  ");
  if (i<10) System.out.print(" ");
  else System.out.print("");
  for(double j=1;j<=10;j++){
    double div=(j/i);
    System.out.print(df.format(div));
    if (div < 10) System.out.print("   ");
  }
  System.out.println();

this prints
   1      2      3      4      5      6      7      8      9      10
1   1   2   3   4   5   6   7   8   9   10
2   0.5   1   1.5   2   2.5   3   3.5   4   4.5   5   
3   0.33   0.67   1   1.33   1.67   2   2.33   2.67   3   3.33   
4   0.25   0.5   0.75   1   1.25   1.5   1.75   2   2.25   2.5   
5   0.2   0.4   0.6   0.8   1   1.2   1.4   1.6   1.8   2   
6   0.17   0.33   0.5   0.67   0.83   1   1.17   1.33   1.5   1.67   
7   0.14   0.29   0.43   0.57   0.71   0.86   1   1.14   1.29   1.43   
8   0.12   0.25   0.38   0.5   0.62   0.75   0.88   1   1.12   1.25   
9   0.11   0.22   0.33   0.44   0.56   0.67   0.78   0.89   1   1.11   
10  0.1   0.2   0.3   0.4   0.5   0.6   0.7   0.8   0.9   1   

i want to print everything as x.xx or neatly in columns 
1 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00  10.0
2 0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00 4.50  5.00
3 0.33 0.67 1.00 1.33 1.67 2.00 2.33 2.67 3.00  3.33
etc

解决方案

Many ways, but one is to use String.format("%.2f", myDouble) Another way is to use a DecimalFormat such as DecimalFormat format = new DecimalFormat("#0.00") and then use it like format.format(myDouble)

To format a table, use String.format(...) or one of its variants like printf.

e.g.

public class Foo2 {
   private static final int COLS = 10;
   private static final int ROWS = 10;

   public static void main(String[] args) {
      System.out.printf("%2s", "");
      for (int i = 0; i < COLS; i++) {
         System.out.printf("%8d", (i + 1));
      }
      System.out.println();
      for (int i = 0; i < ROWS; i++) {
         int row = i + 1;
         System.out.printf("%2d", row);
         for (int j = 0; j < ROWS; j++) {
            double myDouble = (double) row / (j + 1);
            System.out.printf("%8.2f", myDouble);
         }
         System.out.println();
      }
   }
}

which prints out

         1       2       3       4       5       6       7       8       9      10
 1    1.00    0.50    0.33    0.25    0.20    0.17    0.14    0.13    0.11    0.10
 2    2.00    1.00    0.67    0.50    0.40    0.33    0.29    0.25    0.22    0.20
 3    3.00    1.50    1.00    0.75    0.60    0.50    0.43    0.38    0.33    0.30
 4    4.00    2.00    1.33    1.00    0.80    0.67    0.57    0.50    0.44    0.40
 5    5.00    2.50    1.67    1.25    1.00    0.83    0.71    0.63    0.56    0.50
 6    6.00    3.00    2.00    1.50    1.20    1.00    0.86    0.75    0.67    0.60
 7    7.00    3.50    2.33    1.75    1.40    1.17    1.00    0.88    0.78    0.70
 8    8.00    4.00    2.67    2.00    1.60    1.33    1.14    1.00    0.89    0.80
 9    9.00    4.50    3.00    2.25    1.80    1.50    1.29    1.13    1.00    0.90
10   10.00    5.00    3.33    2.50    2.00    1.67    1.43    1.25    1.11    1.00

这篇关于如何格式化打印双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆