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
Moderators: Mug UK, Silver Surfer, Moderator Team
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
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)
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