Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Examples – String Formatting

Java Example – String formatting

Java Examples Java Examples

The following example demonstrates formatting strings with the format() method; a locale can also be specified for formatting:

StringFormat.java file

import

java

.

util

.*;

public

class

StringFormat

{

public

static

void

main

(

String

[

]

args

)

{

double

e

=

Math

.

E

;

System

.

out

.

format

(

"

%f%n

"

,

e

)

;

System

.

out

.

format

(

Locale

.

CHINA

,

"

%-10.4f%n%n

"

,

e

)

;

//

Specify the locale as China (CHINA)

}

}

The output of the above code example is:


2.718282
2.7183  

Java Examples Java Examples