Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 四舍五入到小数点后2位_Java_Android - Fatal编程技术网

Java 四舍五入到小数点后2位

Java 四舍五入到小数点后2位,java,android,Java,Android,我需要输出e4和e5到小数点后2位,因为它是货币 public void afterTextChanged(Editable s) { if (e2.getText().toString() != "") {

我需要输出e4和e5到小数点后2位,因为它是货币

public void afterTextChanged(Editable s) {                                          
        if (e2.getText().toString() != "")                                              
        {                                                                               
            double salePrice = Double.parseDouble(e2.getText().toString());             
            double ebayFee = salePrice / 10.00;                                         
            double paypalFee = (salePrice * 0.034) + 0.2;                               
            double roundedPPFee = Math.round(paypalFee*100.00)/100.00;                  
            double roundedEbayFee = Math.round(ebayFee*100.00)/100.00;                  
            e4.setText(String.valueOf(roundedEbayFee));                                 
            e5.setText(String.valueOf(roundedPPFee));                                   

        }                                                                               
    }                                   

您得到的电流输出是多少

也可以试试这个并检查输出。不用.00,只需添加.0即可

        double roundedPPFee = Math.round(paypalFee*100.0)/100.0;                  
        double roundedEbayFee = Math.round(ebayFee*100.0)/100.0;    
您可以使用(可能与)

或:

你可能想看看类似的问题在哪里得到了广泛的回答


另一方面,您可能不应该将钱存储在double中,因为它们无法准确地表示十进制值-有关更广泛的解释,请参阅。

由于这可以将小数点限制为2位,因此如何将输出设置为2位小数,即使它是一个整数?例如,我希望100显示为100.00,请尝试此.toFixed(2)
        double ebayFee = salePrice / 10.00;
        double paypalFee = (salePrice * 0.034) + 0.2;
        double roundedPPFee = Math.round(paypalFee*100.0)/100.0;
        double roundedEbayFee = Math.round(ebayFee*100.0)/100.0;
        DecimalFormat f = new DecimalFormat("##.00");
        System.out.println(f.format(roundedPPFee));
        System.out.println(f.format(roundedEbayFee));
    //double yourNumber = ...;
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.FLOOR);
    String roundedValue = df.format(yourNumber);
    //double yourNumber = ...;
    String roundedValue = BigDecimal.valueOf(yourNumber).setScale(2, BigDecimal.ROUND_FLOOR).toString();