-1

error

While I'm using truncating in this method, it is somewhere wrong as in picture

    public static List<Double> interp(DoubleUnaryOperator f, double l, double u, int n) {
        double d = (u - l)/n;
        List<Double> result = new ArrayList<>(n);
        double net = f.applyAsDouble(l);
        result.add(net);
        int i = 0;
        while(i+1 < n){

        result.add(Math.floor(f.applyAsDouble(l + d)*100)/100);
        l += d;

        i++;

       }

    return result;
   }

Who know how to truncate correclty? P.S. its important for the number be in two decimal places.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    Can you please post the expected and actual output here as text instead of linking an image? – Kartik Nov 02 '20 at 23:19

1 Answers1

0

Use this :

 new DecimalFormat("#.##").format(dblVar);
           df.setRoundingMode(RoundingMode.DOWN);
 s = df.format(d);

and import the class java.text.DecimalFormat.

user207421
  • 305,947
  • 44
  • 307
  • 483
YourHelper
  • 703
  • 7
  • 17