How to use formatted output floating point numbers in java without displaying excess zeros

println can not output redundant 0
, while printf is very difficult to control, because there is no way to set the number of digits without setting the position after the decimal point without knowing the number of floating-point digits output.

what if there are many zeros after 6 digits by default?

Mar.10,2021

there seems to be no way to control it.

printf is formatted output, and since it is formatted, of course you have to specify a clear format (forced interpretation of a wave, representing only your personal opinion?).


BigDecimal learn about the floating point type in


Java often cannot guarantee its accuracy when calculating, so there is a long list of decimals that output a confusing decimal number when the result is obvious. Therefore, when paying attention to floating point operation, we recommend using BigDecimal ( Note: a constructor with string parameters must be used ). In this way, the accuracy of the calculation can be guaranteed, and the accuracy can be controlled. The landlord can take a look at this class API.

Menu