## how to do percent in java ?

**Moderators:** Mug UK, Silver Surfer, Moderator Team

### how to do percent in java ?

Hi all,

Does anyone know how to do percent values in java ?

for example I want to find 58% of 1192 , both figures alter, I have them variables, but im not to great on java

chris

Does anyone know how to do percent values in java ?

for example I want to find 58% of 1192 , both figures alter, I have them variables, but im not to great on java

chris

### Re: how to do percent in java ?

In any language, the maths is the same. A percentage value is a certain number of hundredths of a value. Like 47% of x is 47/100 times the value of x and so on.exxos wrote:Hi all,

Does anyone know how to do percent values in java ?

As has been mentioned elsewhere in the thread,exxos wrote: for example I want to find 58% of 1192 , both figures alter, I have them variables, but im not to great on java

Code: Select all

```
int pcamount=58;
int value=1192;
int amountofvalue=(value * pcamount)/100
```

If you've got these in a GUI and/or you're using Integer objects instead of ints, then you'll have to take that into account as well. I haven't played with Java much since 1.5 came out with its fancy facilities...

... you add one half to the figure before you int it, or +50 before you divide by 100. In basic one uses the cint() operator.

Code: Select all

```
int pcamount=58;
int value=1192;
int amountofvalue=(value * pcamount +50)/100
```

Try:exxos wrote:have another problem

I have a alue of 10 and want to ADD 10 but it comes out as 1010, seems like a string operation, though if I devide by 10 then it moves the figure into the decimal place like 10.10 ... the line is...

CoilCapacitance = ((form.toroid.value)+CoilCapacitance)

Code: Select all

```
CoilCapacitance = ((Integer.toInt(form.toroid.value)) + CoilCapacitance)
```

CoilCapacitance = (xx +CoilCapacitance);

I found that on google, assume its the same thing as you suggested, It hink me form value is a string hense the conversion... though odd that I did not have these problem with the other math functions :S

The issue you're having is that the form input field stores text as a string. When Java sees (string + number) it converts the number to a string and joins them together. You need to make it see (number + number) to get a number, hence the parseInt.

You might be able to do something to automatically get the form input field as a number instead. It would depend entirely on how the toolkit you're using does its forms.

I have several input boxes, they all do maths but they all work fine, I wonder if this problem only shows up with the plus, like doing a sqrt on a number or string aray will automatically convert to a number array kinda thing..

Actually I didn't write 80% of the program, Just adding more features and more functions, so taking me all day to add 2 numbers so far

form.insulation.value holds 0.00000058

form.CoilPitch.value holds 8

I can't get these to add, the way I did it before kills the first number to zero. adding them ends up with "80.00000058" .

If I convert them to numbers it resets to zero, so how do I get about this one ?!

Chris

Looks scabby though; I'd just go with "Math.round(x * y / 100.0);"Sarek wrote:of course, to round things to the nearest %, instead of rounding down (eg 199/100 = 1%)

... you add one half to the figure before you int it, or +50 before you divide by 100. In basic one uses the cint() operator.

Just a suggestion.Code: Select all

`int pcamount=58; int value=1192; int amountofvalue=(value * pcamount +50)/100`

tá'n poc ar buile!