70 likes | 222 Views
QMCS 230 – Today in Class. Getting Homework Back DecimalFormat class - recap the printf method Revision to Payroll - Lab 6. DecimalFormat class. Convert numbers into strings NICELY Control decimal appearance Insert commas, force/suppress leading 0s import java.text.DecimalFormat;
E N D
QMCS 230 – Today in Class • Getting Homework Back • DecimalFormat class - recap • the printf method • Revision to Payroll - Lab 6 R. Smith - University of St Thomas - Minnesota
DecimalFormat class • Convert numbers into strings NICELY • Control decimal appearance • Insert commas, force/suppress leading 0s • import java.text.DecimalFormat; • DecimalFormat twoDecimals = • new DecimalFormat(“#0.00”); R. Smith - University of St Thomas - Minnesota
Decimal Format Options • 0 = where you ALWAYS want a digit • Digits to the right indicate rounding • . = Decimal point, if any • # = Optional leading digit • , = Comma, like between 3 0s • “#,##0.00” = commas, always one digit to • the left of the decimal, exactly 2 digits to the right of the decimal • % = at end makes percentage (x100) • Let’s do an example R. Smith - University of St Thomas - Minnesota
the printf method • A variant of println that does formatting • Descended from an ill-behaved Unix feature • I only like it because • I know it • It’s easy to use • Bad thing – it only works on output • DecimalFormat works for all strings R. Smith - University of St Thomas - Minnesota
The printf secret code • Make a text string • Embed a % code for each number inserted • d = integer decimal • s = string • f = double floating point • Leading digit = # spaces to use • Comma = insert commas in a number • Decimal followed by digit = #decimal places R. Smith - University of St Thomas - Minnesota
Revision to Payroll • Pick a version (Scanner version is best) • If there’s any overtime, ask for authorization • ASK ONLY ONCE • Remember the decision • If not authorized, pay for first 8 hours • Format the numbers nicely • Calculate to cents • Suppress ill-behaved decimal points R. Smith - University of St Thomas - Minnesota
Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. R. Smith - University of St Thomas - Minnesota