increasing a number by a percentage

Page 1 of 1

14 Replies - 1081 Views - Last Post: 28 January 2013 - 04:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309814&amp;s=c5e3a2b696424cbd6663dd6d4a3f59c3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 dsnowdon

Reputation: 0
• Posts: 221
• Joined: 18-January 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

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:07 AM

DkSnowdon, 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

#3 dsnowdon

Reputation: 0
• Posts: 221
• Joined: 18-January 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 02:10 AM

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

DkSnowdon, 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

#4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

#5 dsnowdon

Reputation: 0
• Posts: 221
• Joined: 18-January 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.

#6 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

#7 dsnowdon

Reputation: 0
• Posts: 221
• Joined: 18-January 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

#8 Flukeshot

• A little too OCD

Reputation: 417
• 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

```

#9 Mylo

• Knows all, except most.

Reputation: 265
• 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

#10 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,483
• 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

#11 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

#12 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,483
• Joined: 12-December 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:16 AM

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

#13 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

#14 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• 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);

```

#15 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,483
• Joined: 12-December 12

Re: increasing a number by a percentage

Posted 28 January 2013 - 04:27 AM

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

Thank you for that, Andrew />

regards,
Raghav

No worries