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

# Thread: First timer here! Help with very very basic problem

1. ## First timer here! Help with very very basic problem

Hello my name is Brandon and I am taking intro to Java for the first time and I do not have and previous experience with programming.

I am stuck on a HW problem:

1) Sales Prediction - The East Coast sales division of a company generates 62 percent of total sales. Based on that percentage, write a program that will predict how much the East Coast division will generate if the company has \$4.6 million in sales this year.

The basic program I wrote seems to not be working and could use some help.

public class SalesPrediction
{
public static void main(String[] args)
{
int x = 4600000;
int y = 0.62;

int total = x * y;

System.out.println("The East Coast will generate \$ " + total + " this year. ");
}
}

The error I am getting says possible loss of precision.

Again I am a complete beginner so any help would be great! Thanks in advance

2. ## Re: First timer here! Help with very very basic problem

int y = 0.62;
Is not valid. An integer (int) cannot contain a decimal number. If you want to use a decimal number, you need to use a double
double y = 0.62;

Also, your int total needs to be a double total, or the fractional part of the x * y equation will be lost.

3. ## The Following User Says Thank You to aussiemcgr For This Useful Post:

brobertson300 (January 31st, 2014)

4. ## Re: First timer here! Help with very very basic problem

Thank you so much! Do you think this program is valid; would you have done it another way? Im trying to learn as much as possible and just pick peoples brain.

5. ## Re: First timer here! Help with very very basic problem

Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

6. ## The Following User Says Thank You to GregBrannon For This Useful Post:

brobertson300 (January 31st, 2014)

7. ## Re: First timer here! Help with very very basic problem

For your purposes, it seems fine. If you wanted to "go the extra mile" and pretty up the output a bit, you could format the total appropriately before you print it. By that, I mean you could make it so only the first two decimal places are displayed (since that is usually how you would see money displayed). You can do that quite easily with the NumberFormat class.
Here is a brief example of how it works:
```double money = 100.25789;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println("The money amount is "+moneyString); // This will print out: The money amount is \$100.26```

Another common practice, is not using double for money. Since you are probably not dealing with "real" money, it isn't an issue. But in real-world applications where money precision is important (like an application for a Bank), the BigDecimal class is recommended by most people. This is because there can be small mathematical mistakes with doubles due to something called "floating-point arithmetic" and because the computer cannot always accurately store a base 10 decimal number as bits. An example of this error can be seen if you run this basic code:
```double number = 0.1 * 0.1;
System.out.println(number);```
We know that the answer to that should be 0.01, but due to how the computer stores the number "0.1", the computer will give the result: 0.010000000000000002
It's just a fun little fact you might see on an exam or something some day, lol.

8. ## The Following User Says Thank You to aussiemcgr For This Useful Post:

brobertson300 (January 31st, 2014)