0% found this document useful (0 votes)
37 views17 pages

Ch4-1 Formatting Decimal

The document discusses formatting real numbers in Java using printf and formatting specifiers like %f. It covers minimum field width, precision, and flags like comma separators. The String.format method can also be used to format output strings.

Uploaded by

Rommel Dorin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views17 pages

Ch4-1 Formatting Decimal

The document discusses formatting real numbers in Java using printf and formatting specifiers like %f. It covers minimum field width, precision, and flags like comma separators. The String.format method can also be used to format output strings.

Uploaded by

Rommel Dorin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

COMPROG1

Program Logic
Formulation
using JAVA
LESSON 4-1

ROMMEL L. DORIN
Contents

Formatting Real numbers

Input using jOption Class


Formatting Output
General Syntax:

Where:
• FormatString is a string that contains text,
special formatting specifiers, or both.
• ArgumentList is a list of zero or more additional
arguments, which will be formatted according to
the format specifiers listed in the format string.
Formatting Output
Example:

Using printf in its simplest form without any


arguments. Similar to print and println
Formatting Output
Printf command %f

%f – is a format specifier.
f – for floating numbers( float, double)
d – for integer values
Format specifier - is a placeholder for a value that
will be inserted into the string when it is displayed.
Formatting Output
Example 1: %f

%f – is a format specifier.
Format specifier - is a placeholder for a value that
will be inserted into the string when it is displayed.
Formatting Output
Example 2: %f %f
Formatting Output
Syntax
The items that appear inside brackets are optional.

 %—All format specifiers begin with a % character.


 flags—After the %character, one or more optional flags may appear.
Flags cause the value to be formatted in a variety of ways.
 width—After any flags, you can optionally specify the minimum field
width for the value.
 .precision—If the value is a floating-point number, after the minimum
field width, you can optionally specify the precision. This is the number
of decimal places that the number should be rounded to.
 conversion—All format specifiers must end with a conversion
character, such as f for floating-point, or d for decimal integer.
Formatting Output
Flags
There are several optional flags that you can insert
into a format specifier to cause a value to be
formatted in a particular way.

Most common is to display numbers separated by


comma.
Formatting Output
Flags
Comma Separators

Output:
Formatting Output
Flags
Comma Separators

Output:
Formatting Output
Minimum Field Width
Sample Code:

Output:
Formatting Output
Combining Minimum Field Width
and precision
Sample Code:

Output:
Formatting Output
Precision
Sample Code:

Output:
Formatting Output
String.Format Method
Use to format strings in Joption -
MessageDialog

General Syntax:
Formatting Output
String.Format Method

You might also like