Simple Java Problem

Discussion in 'JavaScript' started by bstunt, Jun 3, 2010.

  1. #1
    public class Code
    {
    	public static void main(String[] args)
    	{
    	double dblFivePointTwo = 5.2;
    	double dblFourPointFive = 4.5;
    		
    	    System.out.println("4.6 * 5.2 = " + (dblFourPointSix * dblFivePointTwo));
                //  It prints "4.6 * 5.2 = 23.919999999999998"
                //  How can I make the answer round off to 23.92?
    
    	}
    }
    Code (markup):

     
    bstunt, Jun 3, 2010 IP
  2. d_s

    d_s Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    you should be using the Math.round function which will take the number and the number of digits after the decimal places as argument and produce the output as the number with rounded off values.

    regards

    d_s
    http://www.dsaravanan.wordpress.com/javascript.

     
    d_s, Jun 3, 2010 IP