Is it possible to cast with precision in java without using the formatted printf? [duplicate]
问题 This question already has answers here : printf %f with only 2 numbers after the decimal point? (8 answers) Closed 3 months ago . This line results in double value 3.33333333335 System.out.println("Average marks of " + name + " = " + (double)sum/3); Is it possible to set a width of precision? 回答1: You can use DecimalFormat or BigDecimal as follows: import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class Main { public static void main(String[]