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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea0202-47868.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea0202-47868.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?