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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Casting from a whole number to a percent

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #3
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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.

Similar Threads

  1. Explicit Casting?
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 14th, 2013, 06:44 PM
  2. [SOLVED] Get percent of failing grades from an array
    By thom4065 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2013, 04:54 PM
  3. Casting Theory
    By BigDru in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 08:21 AM
  4. Dymaic Casting of List
    By jaypee81 in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2012, 04:46 PM
  5. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 08:13 AM