how do I round off?...

Beginner programmer needing help... :-/

How do I round off a double to make an int?

Typecasting only truncates numbers...

Here's an example of what I want to do...

"2.25356353% of the sum, rounded off, is 2%"

OR

"2.25356353% of the sum, rounded off, is 2.25%"

I want to learn how to round off numbers at the exact decimal places I want them to round off, essentially using significant figures. Is this possible with Java? It is with some other languages...

[506 byte] By [masterdeath01a] at [2007-10-2 11:24:20]
# 1
java.lang.Math.round()
Laszlo.a at 2007-7-13 4:29:16 > top of Java-index,Java Essentials,Java Programming...
# 2

What you want to do isn't actually round the number is it? What you want to do is display the number in a format with fewer digits. There is a class specially for this: DecimalFormat in the package java.text. And if you are using Java you can use the Formatter class in java.util or the new printf methods.

import java.text.DecimalFormat;

....

DecimalFormat formatter = new DecimalFormat("0.##"); // round to two decimals

String roundedNumber = formatter.format(2.25356353); // gives "2.25"

jsalonena at 2007-7-13 4:29:16 > top of Java-index,Java Essentials,Java Programming...