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: Casting from a whole number to a percent

1. ## Casting from a whole number to a percent

Hi,

I had a question about data type casting. Is it possible to take an integer of say 25, and convert that into being a percent so 0.25? How would that look? Where could i go to kind of grasp how to do it?

2. ## Re: Casting from a whole number to a percent

Casting from one data type to another is not the same as dividing an integer by 100.0 and obtaining a double result. This could be an interesting and simple program to demonstrate both casting and the use of various data types in the equation to determine what must be done with it or to it to obtain the desired results.

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

///M Dood (September 22nd, 2013)

4. ## Re: Casting from a whole number to a percent

Wow, i just got that first try. Thank you! Code below for reference. You da man Greg

```class Retirement
{
public static void main(String[] args)
{
int age = 30;
double salary = 30000.0;
int limit = 0;
String sp = "          ";

System.out.println("---------------------------------------------------------------------------");
System.out.println("Age      Salary (\$)   Limit (%)     Contri. (\$)     Cumul. Contribution (\$)");
System.out.println("---------------------------------------------------------------------------");

do
{
if (age > 50)
{
limit = 40;
}
else if (age >40 && age <= 50)
{
limit = 30;
}
else
{
limit = 25;
}
if (age > 30)
{
salary = (salary + (salary * 0.03));
}
int contribution = (int)((salary / 100) * (int)limit);
if (age % 5 == 0)
{
System.out.println(age + sp + (int) salary + sp + limit + sp + contribution );
}
age++;
} while (age <= 60);
}
}```

--- Update ---

It won't let me edit my post, but int contribution is what i was trying to do.