# increasing a number by a percentage

Page 1 of 1## 14 Replies - 832 Views - Last Post: 28 January 2013 - 04:27 AM

### #1

# increasing a number by a percentage

Posted 28 January 2013 - 02:02 AM

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

any help is appreciated

Thanks dale

##
**Replies To:** increasing a number by a percentage

### #2

## Re: increasing a number by a percentage

Posted 28 January 2013 - 02:07 AM

### #3

### #4

## Re: increasing a number by a percentage

Posted 28 January 2013 - 02:11 AM

regards,

Raghav

### #5

## Re: increasing a number by a percentage

Posted 28 January 2013 - 02:22 AM

20% increate on 495

thanks for the help by the way.

### #6

## Re: increasing a number by a percentage

Posted 28 January 2013 - 02:51 AM

regards,

Raghav

### #7

## Re: increasing a number by a percentage

Posted 28 January 2013 - 02:55 AM

### #8

## Re: increasing a number by a percentage

Posted 28 January 2013 - 03:32 AM

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

## Re: increasing a number by a percentage

Posted 28 January 2013 - 03:35 AM

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

## Re: increasing a number by a percentage

Posted 28 January 2013 - 03:54 AM

Quote

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

## Re: increasing a number by a percentage

Posted 28 January 2013 - 04:07 AM

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

## 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

## Re: increasing a number by a percentage

Posted 28 January 2013 - 04:21 AM

regards,

Raghav

### #14

## Re: increasing a number by a percentage

Posted 28 January 2013 - 04:24 AM

Else I don't see your problem

My suggestion was doing it like andrewsw has already shown

value = (int) (value * 1.2);

### #15

## Re: increasing a number by a percentage

Posted 28 January 2013 - 04:27 AM