Download presentation
Presentation is loading. Please wait.
1
March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230 – Today in Class Getting Homework BackGetting Homework Back DecimalFormat class - recapDecimalFormat class - recap the printf methodthe printf method Revision to Payroll - Lab 6Revision to Payroll - Lab 6
2
March 2005 2/18R. Smith - University of St Thomas - Minnesota DecimalFormat class Convert numbers into strings NICELYConvert numbers into strings NICELY Control decimal appearanceControl decimal appearance Insert commas, force/suppress leading 0sInsert commas, force/suppress leading 0s import java.text.DecimalFormat;import java.text.DecimalFormat; DecimalFormat twoDecimals =DecimalFormat twoDecimals = –new DecimalFormat(“#0.00”);
3
March 2005 3/18R. Smith - University of St Thomas - Minnesota Decimal Format Options 0 = where you ALWAYS want a digit0 = where you ALWAYS want a digit –Digits to the right indicate rounding. = Decimal point, if any. = Decimal point, if any # = Optional leading digit# = Optional leading digit, = Comma, like between 3 0s, = 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 decimalthe left of the decimal, exactly 2 digits to the right of the decimal % = at end makes percentage (x100)% = at end makes percentage (x100) Let’s do an exampleLet’s do an example
4
March 2005 4/18R. Smith - University of St Thomas - Minnesota the printf method A variant of println that does formattingA variant of println that does formatting Descended from an ill-behaved Unix featureDescended from an ill-behaved Unix feature I only like it becauseI only like it because –I know it –It’s easy to use Bad thing – it only works on outputBad thing – it only works on output –DecimalFormat works for all strings
5
March 2005 5/18R. Smith - University of St Thomas - Minnesota The printf secret code Make a text stringMake a text string Embed a % code for each number insertedEmbed a % code for each number inserted –d = integer decimal –s = string –f = double floating point Leading digit = # spaces to useLeading digit = # spaces to use Comma = insert commas in a numberComma = insert commas in a number Decimal followed by digit = #decimal placesDecimal followed by digit = #decimal places
6
March 2005 6/18R. Smith - University of St Thomas - Minnesota Revision to Payroll Pick a version (Scanner version is best)Pick a version (Scanner version is best) If there’s any overtime, ask for authorizationIf there’s any overtime, ask for authorization –ASK ONLY ONCE –Remember the decision –If not authorized, pay for first 8 hours Format the numbers nicelyFormat the numbers nicely –Calculate to cents –Suppress ill-behaved decimal points
7
March 2005 7/18R. 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.