public class doubleinstring
{
public static void main(String args[])
{
System.out.println("Inside main");
boolean returnvalue=comparechk(".21999" , ".22");
System.out.println("equal true else false for .21999 , .22 ::"+returnvalue);
returnvalue=comparechk(".2" , "0.2");
System.out.println("equal true else false for .2 and 0.2 ::"+returnvalue);
returnvalue=comparechk("2" , "2.0");
System.out.println("equal true else false for 2 and 2.0 ::"+returnvalue);
returnvalue=comparechk("0.44500000000000001" , ".445");
System.out.println("equal true else false for 0.44500000000000001 and .445 ::"+returnvalue);
returnvalue=comparechk("0.29499999999999998" , ".295");
System.out.println("equal true else false for 0.29499999999999998 and .295 ::"+returnvalue);
returnvalue=comparechk("234.33" , "234.329999");
System.out.println("equal true else false for 234.33 and 234.329999 ::"+returnvalue);
returnvalue=comparechk("0.0" , "0");
System.out.println("equal true else false for 0 & 0.0 ::"+returnvalue);
returnvalue=comparechk("" , "");
System.out.println("equal true else false for empty & empty ::"+returnvalue);
}
public static boolean comparechk(String a, String b)
{
boolean result=false;
if(!a.equals("")&&!a.equals(""))
{
Double s= Double.valueOf(a);
BigDecimal bd1 = new BigDecimal(s);
bd1 = bd1.setScale(3, BigDecimal.ROUND_HALF_UP);
System.out.println("s rounded value ::"+bd1);
Double s1= Double.valueOf(b);
BigDecimal bd = new BigDecimal(s1);
bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
System.out.println("s1 value ::"+bd);
if(bd1.equals(bd))
{
result= true;
}
}
return result;
}
}
No comments:
Post a Comment