I suggest you use BigDecimal
instead of float
, as that's a more natural representation where decimal digits make sense.
However, I don't think you actually want the round
method, as that deals with precision instead of scale. I think you want setScale
, as demonstrated here:
import java.math.*;
public class Test {
public static void main(String[] args) {
roundTo2DP("74.126");
roundTo2DP("74.125");
}
private static void roundTo2DP(String text) {
BigDecimal before = new BigDecimal(text);
BigDecimal after = before.setScale(2, RoundingMode.HALF_DOWN);
System.out.println(after);
}
}