JAVA Format

JAVA Format

The java string format() method returns the formatted string by given locale, format and arguments. If you don’t specify the locale in String. format() method, it uses default locale by calling Locale. … The format() method of java language is like sprintf() function in c language and printf() method of java language.

Java String format()

The java string format() method returns the formatted string by given locale, format and arguments.

If you don’t specify the locale in String.format() method, it uses default locale by calling Locale.getDefault() method.

The format() method of java language is like sprintf() function in c language and printf() method of java language.

Parameters

locale : specifies the locale to be applied on the format() method.

format : format of the string.

args : arguments for the format string. It may be zero or more.


Returns

formatted string


Throws

NullPointerException : if format is null.

IllegalFormatException : if format is illegal or incompatible.


Java String format() method example

  1. public class FormatExample{

2. public static void main(String args[]){

3. String name=”sonoo”;

4. String sf1=String.format(“name is %s”,name);

5. String sf2=String.format(“value is %f”,32.33434);

6. String sf3=String.format(“value is %32.12f”,32.33434);//returns 12 char fractional part filling with 0

7. System.out.println(sf1);

8. System.out.println(sf2);

9. System.out.println(sf3);

}}

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

1 day ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago