How can I truncate a double to only two decimal places in Java? [duplicate]
问题 This question already has an answer here: How to round a number to n decimal places in Java 31 answers For example I have the variable 3.545555555, which I would want to truncate to just 3.54. 回答1: If you want that for display purposes, use java.text.DecimalFormat: new DecimalFormat("#.##").format(dblVar); If you need it for calculations, use java.lang.Math: Math.floor(value * 100) / 100; 回答2: DecimalFormat df = new DecimalFormat(fmt); df.setRoundingMode(RoundingMode.DOWN); s = df.format(d);