java:如何使用printf打印美元符号和2位小数的双精度?

时间:2016-12-07 21:12:45

标签: java string printf double

我对printf真的很糟糕。如何将%$。2f添加到我的String格式中,以便我可以像这样得到一些干净的东西: (我忘了早些时候添加'$')

MP3 Player cost:                        $580.39
Smart Phone cost:                       $510.93
Digital Watch cost:                     $495.99
Tablet cost:                            $643.17
Portable Gaming System cost:            $485.17



import java.util.Scanner;

public class test2 {

    public static void main(String[] arg) {

        Scanner in = new Scanner(System.in);

        int [][] matrix = { // 5 rows, 7 cols
                { 9, 13, 4, 7, 1, 14, 10},
                { 8, 2, 12, 11, 6, 15, 2},
                { 9, 6, 7, 10, 15, 8, 3},
                { 12, 14, 8, 15 ,2 , 7, 8},
                { 12, 10, 3, 11, 8, 3, 5},
        };

        String [] product = new String [5];
        product [0] = "MP3 Player";
        product [1] = "Smart Phone";
        product [2] = "Digital Watch";
        product [3] = "Tablet";
        product [4] ="Portable Gaming System";

        double [] price = {10.75, 15.27, 5.98, 9.67, 4.32, 12.50, 1.42}; // 7 elements

        costOfEach(matrix, product, price);
    }

    // compute and display the cost of manufacturing each device
    public static double costOfEach(int matrix[][], String[] product, double price [] ){
        double cost = 0.00;
        String item = "";
        double maxCost = 0.00;
        double minCost = Double.MAX_VALUE;
        int maxCostIndex = 0;
        int minCostIndex = 0;

这就在这里。我尝试了很多组合但没有结果

        String format = "%-40s%s%n"; 
        for (int row = 0; row < matrix.length; row++){
             cost = 0;
             for (int col = 0, i = 0; col < matrix[0].length && i < price.length; col++, i++){

                 cost += matrix[row][col] * price [i]; 
             }

             System.out.printf(format, product[row] + " cost: $%.2f", cost);
        }
                return cost;
    }

}

2 个答案:

答案 0 :(得分:0)

我做了一些改变。根据您的要求,您的代码应该是这样的:

import java.util.Scanner;

public class test2 {

public static void main(String[] arg) {

    Scanner in = new Scanner(System.in);

    int [][] matrix = { // 5 rows, 7 cols
            { 9, 13, 4, 7, 1, 14, 10},
            { 8, 2, 12, 11, 6, 15, 2},
            { 9, 6, 7, 10, 15, 8, 3},
            { 12, 14, 8, 15 ,2 , 7, 8},
            { 12, 10, 3, 11, 8, 3, 5},
    };

    String [] product = new String [5];
    product [0] = "MP3 Player";
    product [1] = "Smart Phone";
    product [2] = "Digital Watch";
    product [3] = "Tablet";
    product [4] ="Portable Gaming System";

    double [] price = {10.75, 15.27, 5.98, 9.67, 4.32, 12.50, 1.42}; // 7 elements

    costOfEach(matrix, product, price);
}

// compute and display the cost of manufacturing each device
public static double costOfEach(int matrix[][], String[] product, double price [] ){
    double cost = 0.00;
    String item = "";
    double maxCost = 0.00;
    double minCost = Double.MAX_VALUE;
    int maxCostIndex = 0;
    int minCostIndex = 0;

    String format = "%-40s$%.2f%n";
    for (int row = 0; row < matrix.length; row++){
         cost = 0;
         for (int col = 0, i = 0; col < matrix[0].length && i < price.length; col++, i++){

             cost += matrix[row][col] * price [i]; 
         }

         System.out.printf(format, product[row] + " cost:", cost);
    }
            return cost;
}

}

答案 1 :(得分:0)

上限答案是正确的,添加 $ 符号只需编辑后续行:

String format = "%-40s$%.2f%n";

你的输出:

MP3 Player cost:                        $580,39
Smart Phone cost:                       $510,93
Digital Watch cost:                     $495,99
Tablet cost:                            $643,17
Portable Gaming System cost:            $485,17