

Because the precision is specified but not the width, Java will use a field size as large as necessary and totalwill be displayed in a field with 2 positions for decimal digits.totalmustbe a variable of type double or float.If total was 37.05, it would be displayed as:$ 37.05įormatting Numeric Outputusing the printf( ) Method Examples: Floating dollar sign ( $ ) (“The total is $%.2f%n”, total) // precision only, no width.Also, if totalis a big enough number, it will be formatted with commas because the flag ,is used.If you do not make the width large enough, Java will automatically expand it to be as large as necessary. Note: the width must include space for commas and the decimal point. totalwill be displayed in a field 10 positions wide with 2 positions for decimal digits.totalmust be a variable of type double or float because the f format specifier is used.įormatting Numeric Outputusing the printf( ) Method Examples: Fixed dollar sign ( $ ) (“The total is $%,10.2f%n”, total) //This example includes values for for a flag, a width, and a precision countwill be displayed in a field with as many positions as necessary to display the number.countmustbe a variable of type int because the format specifier dis used.The %n causes a newline character to be outputįormatting Numeric Outputusing the printf( ) Method Example: (“The count is %d%n”, count).sumwill be displayed with a decimal point in a field with an many positions as necessary to display the number because no width or precision was specified.summustbe a variable of type double or float because the format specifier fisused.

Include %n in the format string to do this.įormatting Numeric Outputusing the printf( ) Method Example: (“The sum is %f%n”, sum) s//output a string %d //output an integernumber //width specifies the size of a field to use %.f //output a floating-point number //precision specifies the number of decimal digits used //decimal portion is rounded to number of digits %n//output a platform specific newline Note: printf( ) does not print a newline character by default – it is similar to the print( ) method in this regard.

The valid characters which can be used in an identifier are:.Identifier is the generic term for any programmer-created name.* indicates a keyword that is not currently used ** indicates a keyword that was added for Java 2 true, false, and null are not keywords but they are reserved words, so you cannot use them as names in your programs either. These words are reserved-you cannot use any of these words as names in your Java programs. These are HTML based tutorials made available by Sun Microsystems.A practical guide for programmers with hundreds of complete, working examples and dozens of trails-groups of lessons on a particular subject.

general purpose programming language used to create graphical and non-graphical applications and applets.Java Quick Reference Guide prepared by Jack Wilson Cerritos College
