format()

Read(2555) Label: formatted string,

Description:

Generate a formatted string in Java.

Syntax:

format (s,…)

Note:

The function converts data of any type into a formatted string in Java; parameter is the parameter of string s.

Parameter:

s

A string

 

The format parameter of the specified string

Example:

format("my name is %s","wind")

my name is wind

format("this is %f",9552.21) 

this is 9552.210000

format("this is %d",-2)

this is -2

Return value:

A string

Format specifiers in Java:

 

Format Specifier

Conversion Applied

%s

String

%c

Character

%b

Boolean

%d

Decimal integer

%x

Integer hexadecimal

%o

Octal integer

%f

Decimal floating-point

%a

Floating-point hexadecimal

%e

Scientific notation

%g

Causes Formatter to use either %f or %e, whichever is shorter

%h

Hash code

%%

Inserts a percent sign

%n

Inserts a newline character

%tx

Datetime (x is a suffix to describe the portion and precise format of the time or date desired)