CS 121 Lecture Notes - Lecture 16: Type Safety, Enumerated Type

25 views2 pages

Document Summary

Formatting output: it is often necessary to format output values in certain ways so that they can be presented properly: ex: 2. 5%, sh. 025, 0. 025. Numberformat class: allows you to format values as currency or percentages. Decimalformat class: allows you to format values based on a pattern. Both are part of the java. text package. To format currency or percentage values, the numberformat class has 2 static methods that return a formatter output. getcurrencyinstance()- for currency getpercentinstance()- for percentage. Each formatter object has a method called format that returns a string w/ the specified info in the appropriate format: ex: fmt1. format(16,25) . 25. In the same way, if we need to use the percentage format: Numberformat. getpercentinstance(): ex: fmt1. format(0. 256) 25. 6, calculate the final price of a purchase using values entered by the user. Decimalformat class: can be used to format a floating point value in various ways: ex: you can specify that the # should be truncated to 3 decimal places.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions