Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Rounding Question

Hey everybody!

How would I convert the output given to this variable (which is double) into a number rounded to teh nearest 2 numbers after the decimal point.

PST = ((TRSA * (1-SD/100) * PST));

I need it to the nearest number after two decimal points as it's having to do with money.

2. ## Re: Rounding Question

public class z
{
public static void main(String [] args)
{
double x = 3.14159265358979323846;
System.out.printf("x = %.16f\n", x);
System.out.printf("x = %.2f\n\n", x);

double y = 2.7182818284590452354;
System.out.printf("y = %.16f\n", y);
System.out.printf("y = %.2f\n", y);
}
}

Output:

x = 3.1415926535897930
x = 3.14

y = 2.7182818284590450
y = 2.72

Cheers!

Z