Formatting Output. Formatting features of printf System.out.printf can perform the following Rounding floating-point values Aligning properly a column.

Slides:



Advertisements
Similar presentations
printf() Documentation info:
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Entering Data in Excel. Entering numbers, text, a date, or a time n 1Click the cell where you want to enter data. n 2Type the data and press ENTER or.
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Practical PHP IDIA Spring 2012 Bridget M. Blodgett.
Strings PART II STRING$ AND SPACE$. Create strings of specified number String$ creates string of specified character Space$ creates string of spaces Example:
CIS 270—Application Development II Chapter 28—Formatted Output.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Lab Exercises Assigning and Comparing Objects. Class DisneyCharacter Attributes –_name –_year Methods –setAll –getName, getYear –display.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter One Lesson Three DATA TYPES ©
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
INTRODUCTION TO DATABASE USING MICROSOFT ACCESS 2013 Part 5.1 November 16, 2014.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 9 C Formatted Input/Output
ECE Application Programming
TMF1414 Introduction to Programming
OUTPUT STATEMENTS GC 201.
Chapter 08- printf and scanf
Formatting Output.
Lecture 13 Input/Output Files.
Chapter 9 - Formatted Input/Output
Chapter 4 Managing Input and Output Operations
Introduction to Java Applications
CS 1054 Introduction to Programming in Java
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

Formatting Output

Formatting features of printf System.out.printf can perform the following Rounding floating-point values Aligning properly a column of output values Right and left-justification of output Representing Floating-point values in exponential format And integers in Octal and Hexadecimal format Displaying Data with fixed-size and precisions Dates and times in different formats

Printing Integers Conversion CharacterDescription dDisplay a decimal (base 10) integer. oDisplay an octal (base 8) integer. x or XDisplay a base 16 integer.

Printing Floating-Point Numbers Conversion CharacterDescription e or Ea floating-point value in exponential notation. fDisplay a floating-point value in decimal format. g or GDisplay a floating-point value in either the floating-point format f or the exponential format e based on the magnitude of the value. If the magnitude is less than 10–3, or greater than or equal to 107, the floating-point value is printed with e (or E). Otherwise, the value is printed in format f. a or ADisplay a floating-point number in hexadecimal format.

Printing Strings and Characters Conversion CharacterDescription c or CDisplay a char. s or SDisplay a String or any Object

Printing Dates and Times Conversion Suffix CharacterDescription cDisplay date and time formatted as: day month date hour:minute:second time-zone year FDisplay date formatted as year-month-date with four digits for the year and two digits each for the month and date (e.g., ). DDisplay date formatted as month/day/year with two digits each for the month, day and year (e.g., 03/03/04). rDisplay time in 12-hour format as hour:minute:second AM|PM with two digits each for the hour, minute and second (e.g., 04:30:25 PM). RDisplay time formatted as hour:minute with two digits each for the hour and minute (e.g., 16:30). The 24-hour clock is used. TDisplay time as hour:minute:second with two digits for the hour, minute and second (e.g., 16:30:25). The 24- hour clock is used.

Printing Dates and Times (cont’d) Conversion Suffix CharacterDescription ADisplay full name of the day of the week (e.g., Wednesday). aDisplay the three-character name of the day of the week (e.g., Wed). BDisplay full name of the month (e.g., March). bDisplay the three-character short name of the month (e.g., Mar). dDisplay the day of the month with two digits, padding with leading zeros as necessary (e.g., 03). TDisplay the month with two digits, padding with leading zeros as necessary (e.g., 07). eDisplay the day of month without leading zeros (e.g., 3). YDisplay the year with four digits (e.g., 2004). yDisplay the last two digits of the year with leading zeros (e.g., 04). jDisplay the day of the year with three digits, padding with leading zeros as necessary (e.g., 016).

Printing Dates and Times (cont’d) Conversion Suffix CharacterDescription HDisplay hour in 24-hour clock with a leading zero as necessary (e.g., 16). IDisplay hour in 12-hour clock with a leading zero as necessary (e.g., 04). kDisplay hour in 24-hour clock without leading zeros (e.g., 16). lDisplay hour in 12-hour clock without leading zeros (e.g., 4). MDisplay minute with a leading zero as necessary (e.g., 06). SDisplay second with a leading zero as necessary (e.g., 05). ZDisplay the abbreviation for the time zone (e.g., EST or GMT). pDisplay morning or afternoon marker in lowercase (e.g., pm). PDisplay morning or afternoon marker in uppercase (e.g., PM).

Dates and Time Example

Other Conversion Characters Conversion Suffix CharacterDescription b or BPrint "true" or "false" for the value of a boolean or Boolean. These conversion characters can also format the value of any reference. If the reference is non-null, "true" is output; otherwise, "false". When conversion character B is used, the output is displayed in uppercase letters. h or HPrint the string representation of an object’s hash-code value in hexadecimal format. If the corresponding argument is null, "null" is printed. When conversion character H is used, the output is displayed in uppercase letters. %Print the percent character. nPrint the platform-specific line separator (e.g., \r\n on Windows or \n on UNIX/LINUX).

Other Conversion Characters Example

Printing with Field Widths and Precisions Size of a field Specified by a field width value That is inserted between % and the conversion character When Bigger than data => data is right justified Not present => data occupies as many positions as needed Precision When used with e and f => number of digits after decimal point with g, a, or A => number of significant digits to print with s => max number of characters to write from String

Field Width Example

Precision Example

Using Flags Conversion Suffix CharacterDescription - (minus sign)Left justify the output within the specified field. + (plus sign)Display a plus sign preceding positive values and a minus sign preceding negative values. spacePrint a space before a positive value not printed with the + flag. #Prefix 0 to the output value when used with the octal conversion character o. Prefix 0x to the output value when used with the hexadecimal conversion character x. 0 (zero)Pad a field with leading zeros., (comma)Use the locale-specific thousands separator (i.e., ',' for U.S. locale) to display decimal and floating-point numbers.

Flag Examples

Flag Examples (cont’d)

Printing with Argument Indices An argument index Is an optional integer followed by a $ sign Indicating the argument’s position in the argument list Example Argument index “1$” indicates that  Format specifier uses first argument public class ArgumentIndexTest { public static void main( String[] args ) { System.out.printf( "Parameter list without reordering: %s %s %s %s\n", "first", "second", "third", "fourth" ); System.out.printf( "Parameter list after reordering: %4$s %3$s %2$s %1$s\n", "first", "second", "third", "fourth" ); } // end main } // end class ArgumentIndexTest

Formatter class Formatter part of java.util package allows sending formatted data to to a specified destination, such as a file or a disk Example: import java.util.Formatter; import javax.swing.JOptionPane; public class FormatterTest { public static void main( String[] args ){ Formatter formatter = new Formatter(); formatter.format(“%d=%#o=%#X”,10,10,10); JOptionPane.showMessageDialog( null, fromatter.toString()); } // end main } // end class FormatterTest;