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.

Tushar
  • 3,527
  • 9
  • 27
  • 49
Prabu
  • 3,550
  • 9
  • 44
  • 85

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,180
  • 9
  • 21
1

Try something like:

float value = 33000.00f;//float??
String formattedString = String.format( "%.2f", value);
System.out.println(formattedString);
Output:
33000.00 
SMA
  • 36,381
  • 8
  • 49
  • 73