0

multiply the double Type number, from the output need to taken with two Decimal Place

double creditCardPercentage = 0.03;
String renewalEventAmount = "2144.60";
double expectedRenewalAmount = 0;
expectedRenewalAmount = Double.parseDouble(renewalEventAmount) * creditCardPercentage;

the output for the expectedRenewalAmount is 64.338, then how can we format the above output with two decimal

Expected:

64.33
Prabu
  • 3,550
  • 9
  • 44
  • 85
  • 1
    See here http://stackoverflow.com/questions/6447153/decimalformat-rounding – dhke Apr 02 '15 at 06:42
  • @Prabu Are you only concerned with the formatting of the output, or, going with the example you gave, would you be doing further maths with the output? – Tim Biegeleisen Apr 02 '15 at 06:52
  • When dealing with currency its generally not a great idea to use double arithmetic since it isn't precise. It is better to convert them to ints (by multiplying by some multiple of 10 and then divide by a multiple of ten depending on how many decimal places you want. – yitzih Apr 02 '15 at 07:24

7 Answers7

3

You should use the BigDecimal class because it has built-in handling for floating point precision:

BigDecimal creditCardPercentage = new BigDecimal(0.03);
String renewalEventAmountString = "2144.60";
BigDecimal renewalEventAmount = new BigDecimal(renewalEventAmountString);
BigDecimal expectedRenewalAmount = renewalEventAmount.multiply(creditCardPercentage);
expectedRenewalAmount = expectedRenewalAmount.setScale(2, BigDecimal.ROUND_HALF_DOWN);

System.out.println(expectedRenewalAmount); // prints 64.33

One of the advantages of using BigDecimal to handle the formatting is that it allows you to separate your code from your business logic, i.e. rounding down to 2 decimal places, from the view code which outputs the result to the user.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

You could use NumberFormat.getNumberInstance().format(expectedRenewalAmount) which will use the system properties to format the value. You can also modify the NumberFormat, specifying the number of decimal places if you want.

Or you could use System.out.printf or String.format to format the value as well...

String value = String.format("%.2f", expectedRenewalAmount);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

A couple answers have suggested printf; but if you don't want to print the answer right away, you can use String.format the same way:

String formattedString = String.format("%.2f", expectedRenewalAmount);

and now you can print out the result if you want, or you can display it in a Swing text window or change all the zeros to happy faces or do whatever else you like with the resulting string, which you can't do with printf.

ajb
  • 31,309
  • 3
  • 58
  • 84
1

You can use the printf() function with %f:

System.out.printf("%.2f", expectedRenewalAmount);

Here you can find a beautiful printf format cheat sheet by Alvin Alexander that might help you (and hopefully others) a lot.

abarisone
  • 3,707
  • 11
  • 35
  • 54
0

Try this while printing:

System.out.printf("%.2f", expectedRenewalAmount);
agamagarwal
  • 978
  • 7
  • 17
0

You can use

DecimalFormat df = new DecimalFormat("#.00");
double creditCardPercentage = 0.03;
String renewalEventAmount = "2144.60";
double expectedRenewalAmount = 0;
expectedRenewalAmount = df.format(Double.parseDouble(renewalEventAmount) * creditCardPercentage);
praveen_programmer
  • 1,072
  • 11
  • 25
0

you can use minimumFractionDigit to 0

public class Test {

    public static void main(String[] args) {
        System.out.println(format(14.0184849945)); // prints '14.01'
        System.out.println(format(13)); // prints '13'
        System.out.println(format(3.5)); // prints '3.5'
        System.out.println(format(3.138136)); // prints '3.13'
    }

    public static String format(Number n) {
        NumberFormat format = DecimalFormat.getInstance();
        format.setRoundingMode(RoundingMode.FLOOR);
        format.setMinimumFractionDigits(0);
        format.setMaximumFractionDigits(2);
        return format.format(n);
    }

}

please find below reference link.

Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

Community
  • 1
  • 1
Pratik
  • 944
  • 4
  • 20