0

Following are my codes to add two values

float ptoTotalAmount = 0;
Map<String, String> renewalDetailview = new LinkedHashMap<String, String>();
String Fee = driver
                .findElement(
                        By.xpath("//div[@data-bind='html: CURRenewalFees']"))
                .getText().substring(4).replace(",", "");       
        ptoTotalAmount += Float.valueOf((ptoFee));
        String surcharge = driver
                .findElement(By.xpath("//div[@data-bind='html: Surcharges']"))
                .getText().substring(4).replace(",", "");       
        ptoTotalAmount += Float.valueOf((surcharge));

        renewalDetailview.put("totalfee", Float.toString(ptoTotalAmount));

For an Example:

Fee - 31500.00,surcharge - 1500.00

Fee + surcharge = 33000.00

Thus, expected result for totalfee - 33000.00, but I'm getting 33000.0

Therefore, I applied Formatting to my float value with 2 decimal places, but I'm not getting expected result.

DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMinimumFractionDigits(2);

        renewalDetailview.put("totalfee", String.valueOf(df.format(ptoTotalAmount)));

I'm getting 330,00 something like this, please help me to format with 2 decimal places.

Avidan
  • 1,505
  • 2
  • 22
  • 39
Prabu
  • 3,230
  • 9
  • 41
  • 72

2 Answers2

2

It looks like you are trying to model currency. If so, you should check the answers on this post

Community
  • 1
  • 1
Jaimie Whiteside
  • 1,130
  • 9
  • 20
1

Try something like:

float value = 33000.00f;//float??
String formattedString = String.format( "%.2f", value);
System.out.println(formattedString);
Output:
33000.00 
SMA
  • 35,277
  • 7
  • 46
  • 71