0

i need to output e4 and e5 to 2 decimal places as it is currency

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));                                   

        }                                                                               
    }                                   
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108

3 Answers3

1

You can use DecimalFormat (possibly with RoundingMode)

    //double yourNumber = ...;
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.FLOOR);
    String roundedValue = df.format(yourNumber);

or BigDecimal:

    //double yourNumber = ...;
    String roundedValue = BigDecimal.valueOf(yourNumber).setScale(2, BigDecimal.ROUND_FLOOR).toString();

You may want to take a look at the other thread where similar question was answered quite extensively.

On a side note, you probably should not store money in a double because of their inability to represent decimal values accurately - see this question for broader explanation.

kamarius
  • 21
  • 4
0

What is the current output you are getting?

Also try this and check your output. Instead of .00, just add .0

        double roundedPPFee = Math.round(paypalFee*100.0)/100.0;                  
        double roundedEbayFee = Math.round(ebayFee*100.0)/100.0;    
Sagar Morakhia
  • 642
  • 5
  • 12
0
        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));
Mujahid Masood
  • 101
  • 2
  • 7