public static String TaxAmount(String totprice,String tax)
{
String strD2=tax;
double totalprice= Double.parseDouble(strD2);
String strDl = totprice.trim();
if(strDl.contains(","))
strDl = strDl.replace(",", "");
double dbl = Double.parseDouble(strDl);
double db2=(dbl*totalprice)/100;
double Total = Math.round(db2 * 100.0 ) / 100.0;
double TotalTax=Total+dbl;
double Total1 = Math.round(TotalTax * 100.0 ) / 100.0;
String Partmonth=String.valueOf(Total1);
return Partmonth;
}
Here Iam Calculating tax percent from my formulae it is displaying as
27.0 where as In Application it as displaying as 27.00
my Assertion fails at that point anyone can help me this issue
-
Downvote this question since this question has nothing to do with Selenium. Please add selenium related code so that I take my downvote back. You also should not format a question text as a part of code.– Alexey R.Nov 21, 2018 at 13:45
-
Alexey R I would be pretty happy if you can help me with my Question and downvote– John snowNov 21, 2018 at 13:52
-
Okay. So you need to format value from your formula to have 2 decimal positions?– Alexey R.Nov 21, 2018 at 13:53
-
Can you provide some input data examples to your method so that I could test?– Alexey R.Nov 21, 2018 at 13:55
-
Appreciate your Initiation Alexey for e.g. totprice=600.00 and Tax=6.25 my formulae is calculating 637.5 and In application level it is calculating 637.50– John snowNov 21, 2018 at 16:51
1 Answer
Use the DecimalFormat class
DecimalFormat df = new DecimalFormat("##0.00");
String strD2="6.25";
double totalprice= Double.parseDouble(strD2);
String strDl = "1500";
if(strDl.contains(","))
strDl = strDl.replace(",", "");
double dbl = Double.parseDouble(strDl);
double db2=(dbl*totalprice)/100;
double Total = Math.round(db2 * 100.0 ) / 100.0;
double TotalTax=Total+dbl;
double Total1 = Math.round(TotalTax * 100.0 ) / 100.0;
String Partmonth=String.valueOf(df.format(Total1));
System.out.println(Partmonth);