java - junit testing -- how to comparing two ArrayList<Double> using assertEquals(), is there an better alternative? -


i'm writing junit test use assertequals() compare value of 2 arraylist.

@test public void test(){          quadraticformula 1 = new quadraticformula(3, 4, -4);         list<double> results = new arraylist<>();         results.add(0.66666);         results.add(-2.0);          assertequals(results, one.getroots());     } 

however, junit prints out following failure message:

expected:<[0.66666, -2.0]> was:<[0.6666666666666666, -2.0]> junit.framework.assertionfailederror 

is there anyway can specify how many decimal places want test compare? know how test equality of 2 double values accuracy, there anyway can achieved when comparing 2 arraylist of doubles? in advance suggestions or help!

you can use numberformat or bigdecimal

below round-of

    double d = 0.66666666;     numberformat format = new decimalformat("#.##");     system.out.println(format.format(d)); // 0.67     bigdecimal bigdecimal = new bigdecimal(d);     bigdecimal = bigdecimal.setscale(2, bigdecimal.round_half_up);     system.out.println(bigdecimal); // 0.67 

if don't want round-of

    double d = 0.66666666;     decimalformat format = new decimalformat("#.##");     format.setroundingmode(roundingmode.down);     system.out.println(format.format(d)); // 0.66     bigdecimal bigdecimal = new bigdecimal(d);     bigdecimal = bigdecimal.setscale(2, roundingmode.down);     system.out.println(bigdecimal); //0.66 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -