String.format() Output the string as per the required format
String1.format(formats , values )
Here are some formats can be used.

%s String
%f float
%d integer
%c Char
%b Any value
%e Scientific notation
%g decimal with scientific

Example using Integers

int my_num=254;
System.out.println(String.format("No =  %d",my_num)); //No =  254

using double

double No=52.67;
System.out.println(String.format("No =  %f",No)); //No =  52.670000 
System.out.println(String.format("No =  %.4f",No));//No=  52.670
System.out.println(String.format("No =  %.5f",No));//No =  52.67000
System.out.println(String.format("No =  %.6f",22.32));//No = 22.320000
System.out.println(String.format("No =  %.5g",No));//No =  52.670
System.out.println(String.format("No =  %.5b",No));//No =  true
System.out.println(String.format("No =  %.5e",No));//No =  5.26700e+01

Using float

float my_float=52.67f;
System.out.println(String.format("No =  %.5f",my_float));//No =  52.67000
System.out.println(String.format("No =  %.5g",my_float));//No =  52.670

using string

String my_str="plus2net";
System.out.println(String.format("Welcome to %s",my_str)); //Welcome to plus2net

Order of the string output

String my_str1="plus2net";
String my_str2="to";
String my_str3="Welcome";
System.out.println(String.format("%3$s %2$s %1$s ",my_str1,my_str2,my_str3));	
Output is here
Welcome to plus2net
We can add padding to an integer by using format.
int  my_no=25;
System.out.println(String.format("|No=%d|",my_no)); //|No=25|
System.out.println(String.format("|No=%5d|",my_no)); // |No=   25|
System.out.println(String.format("|No=%-5d|",my_no)); //|No=25   |
System.out.println(String.format("|No=%05d|",my_no)); //|No=00025|
All String functions


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer