Results 1 to 4 of 4

Thread: Formatting number in Java

  1. #1

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524

    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.

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    NumberFormatter class, maybe?

  3. #3

    Thread Starter
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524
    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.

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    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
  •  



Click Here to Expand Forum to Full Width