How do I limit the number of decimals printed for a double?

Use a DecimalFormatter:

double number = 0.9999999999999;
DecimalFormat numberFormat = new DecimalFormat("#.00");

Will give you “0.99”. You can add or subtract 0 on the right side to get more or less decimals.

Or use ‘#’ on the right to make the additional digits optional, as in with #.## (0.30) would drop the trailing 0 to become (0.3).

Leave a Comment