14 Replies - 407 Views - Last Post: 28 January 2013 - 04:27 AM Rate Topic: -----

#1 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

increasing a number by a percentage

Posted 28 January 2013 - 02:02 AM

i hava a method that i needs to increase variable by 20% and i am not sure how to do this because in normal math i would just to : ( n / 100 ) * 120

but you cant do that in java because it dosent take into account the remainder

any help is appreciated
Thanks dale
Is This A Good Question/Topic? 0
  • +

Replies To: increasing a number by a percentage

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:07 AM

View PostDkSnowdon, on 28 January 2013 - 02:32 PM, said:

but you cant do that in java because it dosent take into account the remainder


Well, I would like to tell you that there is an operator called mod ( % ) which gives you the remainder in Java

eg. 6 % 4 = 2


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:10 AM

View Postraghav.naganathan, on 28 January 2013 - 02:07 AM, said:

View PostDkSnowdon, on 28 January 2013 - 02:32 PM, said:

but you cant do that in java because it dosent take into account the remainder


Well, I would like to tell you that there is an operator called mod ( % ) which gives you the remainder in Java

eg. 6 % 4 = 2


regards,
Raghav



i still dont understand how to use that to do the calculation i need to do

increase a variable by a percantage
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:11 AM

Can you give me an example of what you want the calculation to do?

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:22 AM

one of the posible option in my program would be

20% increate on 495

thanks for the help by the way.
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:51 AM

So, why don't you do a type casting...so that the value still remains as it is supposed to be, without any change...

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#7 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 31-October 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:55 AM

what is type casting? sorry i am new to java
Was This Post Helpful? 0
  • +
  • -

#8 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 03:32 AM

Wouldn't using a double work for this situation? Decimal fractions should be an acceptable format for a number that doesn't become whole with a 20% increase.

Could be used as follows:
int x = 0; //this will be used as your original number
double result = (x / 100) * 120; //this number is 120% of x


Was This Post Helpful? 0
  • +
  • -

#9 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: increasing a number by a percentage

Posted 28 January 2013 - 03:35 AM

It's best to avoid type casting where possible. If you need to increase a number by a certain percentage, you should store the number as a double. You can then do [num / 100] * percent as usual.

I don't understand what you mean about the remainder though, did you mean decimal?

Edit: To Flukeshot, that piece of code will have a problem with integer division. X will need to also be a double, or divide by '100.0'.

This post has been edited by Mylo: 28 January 2013 - 03:40 AM

Was This Post Helpful? 1
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 03:54 AM

Quote

Edit: To Flukeshot, that piece of code will have a problem with integer division. X will need to also be a double, or divide by '100.0'.


The alternative to
double result = (x / 100.0) * 120;    // added .0

is
double result = x * 1.2;

which seems slightly more reliable. That is, we are not likely to forget the decimal point.

Added: The problem that we are avoiding is that if x is an integer value of 80 then performing x / 100 will use integer arithmetic, producing a value of 0 - which when multiplied by 120 is still 0. Using x / 100.0 avoids this issue, as does multiplying by 1.2. 100.0 and 1.2 are both doubles, forcing Java to use floating-point arithmetic.

This post has been edited by andrewsw: 28 January 2013 - 04:09 AM

Was This Post Helpful? 2
  • +
  • -

#11 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:07 AM

Well, here is an example of what I meant by doing a type casting...

public class MyClass
{
	public static float getPercentFromAmount(int amount) 
	{//As suggested by Andrew(in bold)
	        return (float) (amount + (amount* [b]20/100.0[/b])); //here is where the type casting is taking place 
	}

	public static void main(String args[])
	{
		float a= getPercentFromAmount(100);
		System.out.print(+a);
	}
}


Edit : Optimized some code and made the change suggested by andrewsw

regards,
Raghav

This post has been edited by raghav.naganathan: 28 January 2013 - 04:25 AM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:16 AM

View Postraghav.naganathan, on 28 January 2013 - 04:07 AM, said:

public class MyClass
{
	public static float getPercentFromAmount(int amount) 
	{
	        return (float) ((float) amount + (float) (amount* 20/100)); //here is where the type casting is taking place
	}

	public static void main(String args[])
	{
		float a= getPercentFromAmount(100);
		System.out.print(+a);
	}
}

This has the same issue as the OP. If you run it and enter the value 4 it gives the result 4 rather than 4.8. You can either do:
(amount* 20/100.0)
// or
((float) amount * 20/100)

This post has been edited by andrewsw: 28 January 2013 - 04:17 AM

Was This Post Helpful? 1
  • +
  • -

#13 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:21 AM

Thank you for that, Andrew :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:24 AM

Is it because you need to keep it as an int ?
Else I don't see your problem

My suggestion was doing it like andrewsw has already shown
value = (int) (value * 1.2);


Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:27 AM

View Postraghav.naganathan, on 28 January 2013 - 04:21 AM, said:

Thank you for that, Andrew :)/>

regards,
Raghav

No worries :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1