Monday, August 3, 2009

Formating String using String Class

public class FormatString
{
public static void main(String [] arg)
{
Object ob[] = {"prabir",27};
st = String.format("My name is %s, I am %d years old", ob);
System.out.println(st);

st = String.format("The value of PI is:%f", Math.PI);
System.out.println(st);
//specify no. of decemals
st = String.format("The value of PI is:%.2f", Math.PI);
System.out.println(st);
}
}

Here
  • s expecting string value.
  • d expecting numeric value without decimal.
  • f expecting float value.
  • .2 specify no of decimal places.
Output:
The value of PI is: 3.141593
The value of PI is: 3.14

No comments:

Post a Comment