Java: decimal formatting, continued


The tutor continues with the potentially tricky topic of Java decimal formatting.

 
With yesterday’s post I broke the ice about Java DecimalFormat with a short demonstration program. Now, some key points of explanation:

  1. The format descriptor string “#,###.00” means that two decimal places will always be shown, even if they are zeros. However, the number 0.79 will display as .79 (no zero in front of the decimal point). A “#” means that the place will not be shown if it is a leading or trailing zero, whereas a “0” means the place will be shown, with a zero if it has no other value. Therefore, the number 2, formatted as “#0,000.00”, will be rendered as 0,002.00 by DecimalFormat.
  2. Notice that the output of DecimalFormat is a string (that looks like a number).
  3. The Scanner is not intrinsically connected to DecimalFormat; it is used in the program to read the numbers from the command line.

While this probably covers the program from yesterday, there is still more to say about Java’s DecimalFormat. Not surprisingly, I’ll be continuing about it in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , , , ,

Leave a Reply