-1

Is there any way that i can put this calculation into the price result without decimal.

 private void calculate() {

    double b = Double.parseDouble(berat.getText().toString());

    if (b <= 0.5) {
        totalPrice1 = 7;
        serviceFee1 = totalPrice1 * 0.2;
        carierFee1 = totalPrice1 * 0.8;
    } else {
        totalPrice1 = b * 12.9;
        serviceFee1 = totalPrice1 * 0.2;
        carierFee1 = totalPrice1 * 0.8;
    }

}

for example if i add 0.5 the price result will be in the carrier fee is 5.60000000000. I want that will be display as 5.60.

4 Answers4

1

Try this

DecimalFormat form = new DecimalFormat("0.00");
        String formatedStr = form.format(carrierfee);
prashant17
  • 1,520
  • 3
  • 14
  • 23
0

You can use the DecimalFormat class ex:

DecimalFormat df = new DecimalFormat("#.00");
String result = df.format(input)

This will output 2 decimal points

Link for more info: https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

Nigel Brown
  • 440
  • 2
  • 13
0

Update your function as below:

 private void calculate() {

    double b = Double.parseDouble(berat.getText().toString());

    if (b <= 0.5) {
        totalPrice1 = 7;
        serviceFee1 = totalPrice1 * 0.2;
        carierFee1 = totalPrice1 * 0.8;
    } else {
        totalPrice1 = b * 12.9;
        serviceFee1 = totalPrice1 * 0.2;
        carierFee1 = totalPrice1 * 0.8;
    }
    serviceFee1 = Math.round(serviceFee1 * 100.0) / 100.0;
    carierFee1 = Math.round(carierFee1 * 100.0) / 100.0;
}
Jaydeep Patel
  • 146
  • 2
  • 9
0

There are two ways to display decimal.

  1. DecimalFormat df = new DecimalFormat("#.00"); String result = df.format(carierFee1);

    The result will be : price=0.5 -> result=5.6

  2. String result = String.format( "%.2f", carierFee1 );

    The result will be : price=0.5 -> result=5.60

You can choose one by your purpose.

ld-sr-dev
  • 41
  • 1
  • 5