I was wondering if someone can help me with how to convert my code so that it converts the cents to dollars and also round to the nearest cent:

public class Subscription {				
	private int price;					
	private int length;					

	public Subscription(int p, int n) {
		price = p;
		length = n;
	}

		public double pricePerMonth() {
		double r = ((double) price / (double) length); 		return r;									 
	}


	public void cancel() {
		length = 0;
	}
}

Any help would be most appreciated.

Use division by 100 in combination with the 'int' primitive type to see how many dollars you have. For example,

int dollars = cents/100;

No matter what (int) value you have for cents, at the end of that operation, dollars will be an integer, so it will have the exact amount of dollars. You could have up to 99 cents left over, so you can use the % operator to figure out what your remainder is. I don't know what you mean by "round to the nearest cent", did you mean "round to the nearest dollar"?

modulus: http://en.wikipedia.org/wiki/Modulo_operation

Use division by 100 in combination with the 'int' primitive type to see how many dollars you have. For example,

int dollars = cents/100;

No matter what (int) value you have for cents, at the end of that operation, dollars will be an integer, so it will have the exact amount of dollars. You could have up to 99 cents left over, so you can use the % operator to figure out what your remainder is. I don't know what you mean by "round to the nearest cent", did you mean "round to the nearest dollar"?

modulus: http://en.wikipedia.org/wiki/Modulo_operation

Do you know how to set the length to round appropriately?

Did you even read about modulus? If 100 % yourCents > 50 then you would round up. Otherwise, your value would stay the same. This is because an int value "truncates", so when you previously did

int dollars = cents/100;

it automatically "rounds down" by default (everything is cut off except the integer value, so if you had 1.2 it would end up as 1, and if you had 1.9 it would still be 1). Again, modulus gives you the remainder of division. So if you don't get it then look at some examples online, otherwise, think about what a remainder is.

Did you even read about modulus? If 100 % yourCents > 50 then you would round up. Otherwise, your value would stay the same. This is because an int value "truncates", so when you previously did

int dollars = cents/100;

it automatically "rounds down" by default (everything is cut off except the integer value, so if you had 1.2 it would end up as 1, and if you had 1.9 it would still be 1). Again, modulus gives you the remainder of division. So if you don't get it then look at some examples online, otherwise, think about what a remainder is.

Ok I have tried using the % operator but still getting an error, can you explain how it works:

public double pricePerMonth() {
		double r = ((double) price / (double) length);		r = r % 100;
		return r;					}

I have tried multipling r times 100 around r so (r % 100)*100 but that does not compile.

Ok I have tried using the % operator but still getting an error, can you explain how it works:

public double pricePerMonth() {
		double r = ((double) price / (double) length);		r = r % 100;
		return r;					}

I have tried multipling r times 100 around r so (r % 100)*100 but that does not compile.

I got it using this:

public double pricePerMonth() {
		double r = ((double) price / (double) length); 
		r = r * 100;
		r = Math.round(r);
		r = r/100;
		return r;									
	}

thanks for the help.

No problem, glad you got it working, mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.