|
-
Feb 11th, 2003, 07:05 AM
#1
Thread Starter
Fanatic Member
Formatting number in Java
Can anyone please tell me how to display the number in 7.50 ie. 2 digit after decimal format? I'm utterly confused after searching thru Java documentation.
public class Test
{
public static void main(String args[])
{
System.out.println("Hello World");
double x=50,z;
z=Math.sqrt(x);
System.out.println("The square root of 50 is " +z);
}
}
Thanx
Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.
-
Feb 11th, 2003, 08:36 AM
#2
NumberFormatter class, maybe?
-
Feb 11th, 2003, 09:09 AM
#3
Thread Starter
Fanatic Member
I'm looking for exact syntax/example. I also searched for NumberFormat class but couldn't make it work
Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.
-
Feb 11th, 2003, 09:28 AM
#4
Code:
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.lang.StringBuffer;
import java.text.NumberFormat.Field;
import java.lang.System.*;
class temp
{
public static void main(String [] args)
{
DecimalFormat decFormat = new DecimalFormat("#,##0.00");
StringBuffer buff = new StringBuffer();
decFormat.format(1234.56, buff, new FieldPosition(Field.DECIMAL_SEPARATOR));
System.out.println(buff.toString());
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|