Results 1 to 4 of 4

Thread: Truncate out

  1. #1
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Truncate out

    hi,
    I wrote a small program to compute BMI, but is there a way to truncate the output of this?
    XML Code:
    import java.util.Scanner;
    
    public class ComputeBMI {
        public static void main(String[] arg) {
    
    	//Create an instance of the Scanner
    	Scanner input = new Scanner(System.in);
    
    	    //Input
    	    System.out.print("Enter weight in Pounds: ");
    	double Weight = input.nextDouble();
    	    System.out.print("Enter height in inches: ");
    	    double Height = input.nextDouble();
    
    	    //Math and variables
    	    double BMI = (Weight / (Height * Height)) * 703;
    	   
    	    //Display Result
    	    System.out.println("Your BMI is " + BMI);
        }
    }
    on the output i get Your BMI is 26.854599999999998. How can i make it so i get like 26.8573?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Truncate out

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Truncate out

    Java Code:
    formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
    Information on how to use the Formatter class can be found here:
    Formatter (Java 2 Platform SE 5.0)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Truncate out

    Quote Originally Posted by brynpttrsn View Post
    Java Code:
    formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E);
    Information on how to use the Formatter class can be found here:
    Formatter (Java 2 Platform SE 5.0)
    1. Don't spoonfeed. Especially not bad code.
    2. Don't post links to old EOL Java API. bookmark these:
    Java Platform SE 6
    Java Platform SE 7

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 10-25-2011, 07:29 PM
  2. JList item text does not truncate when it's HTML
    By edsporty in forum New To Java
    Replies: 1
    Last Post: 03-02-2011, 09:09 PM
  3. truncate a decimal number ?
    By hacikho in forum New To Java
    Replies: 5
    Last Post: 10-13-2010, 09:58 PM
  4. To truncate jump of line in jsp
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •