There is a string containing a number with several decimal places. How to convert unnecessary characters when converting to number? Let's say only one decimal is allowed, and there are 2 or 3 in the line. You need to get a number without these extra characters. How to make it easier?

    2 answers 2

    new BigDecimal("35.3456").setScale(4, RoundingMode.HALF_UP); // result = 35.3456 

    setScale(4 - here 4 means the number of decimal places
    RoundingMode.HALF_UP - rounding up

      Another rounding option with N precision: Math.round(y * 100.0) / 100.0;