Reputation: 25
I'm trying to truncate to the third decimal point using printf in Java, but I keep getting to only the second decimal point. Here's the line of code where I was attempting this:
System.out.printf("The speed in ft/sec is %6.2f\n", time);
Upvotes: 2
Views: 2457
Reputation: 236004
Try this:
System.out.printf("The speed in ft/sec is %6.3f\n", time);
The above will round to three decimal places (not "truncate" them). The only difference is in the value after the dot.
Upvotes: 5
Reputation: 360662
Try %6.3f
instead. the format is
%(before).(after)(type)
6 3 f
6 -> 6 digits before the decimal
3 -> 3 digits AFTER the decimal
Upvotes: 7