When i write in my program

double pi = Math.PI;

This will give me a value of PI accurate to many decimal places, but what if i want to limit this number
lets say to 3 decimal places..
Is there such a method in Java?

(Im not talking here only about the value of Math.PI or Math.E, but about all double values that contain
many decimal places)

Do you really want to truncate the value, or do you actually want to print out a value up to three decimal places?

http://java.sun.com/docs/books/tutorial/java/data/numberformat.html
Look at the DecimalFormat example there. You can also use a C-style printf statement if you want to.
You could also use DecimalFormat to truncate the value, btw - I don't know if it is the most efficient way to do so, but it will work.

DecimalFormat f = new DecimalFormat("#.##");
		System.out.println("Enter a double");
		Scanner in = new Scanner(System.in);
		System.out.println(f.format(in.nextDouble()));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.