how to do percent in java ?

No topic. Everything you want to speak about. Please just stay courteous.

Moderators: Mug UK, Silver Surfer, Moderator Team

Post Reply
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

how to do percent in java ?

Post by exxos »

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
User avatar
Cyprian
10 GOTO 10
10 GOTO 10
Posts: 1964
Joined: Fri Oct 04, 2002 11:23 am
Location: Warsaw, Poland

Post by Cyprian »

int x = 1192;
int y = 58;

int result = x*y/100
User avatar
Desty
Atari God
Atari God
Posts: 1970
Joined: Thu Apr 01, 2004 2:36 pm
Location: 53 21N 6 18W
Contact:

Post by Desty »

Cyprian_K wrote:int result = x*y/100
Best not to use ints of course, since so much information would be lost.
i.e. 99/100 = 0, 199/100 = 1.
tá'n poc ar buile!
User avatar
daeghnao
Captain Atari
Captain Atari
Posts: 479
Joined: Wed Oct 27, 2004 12:41 pm
Location: York, UK
Contact:

Re: how to do percent in java ?

Post by daeghnao »

exxos wrote:Hi all,

Does anyone know how to do percent values 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: for example I want to find 58% of 1192 , both figures alter, I have them variables, but im not to great on java :-(
As has been mentioned elsewhere in the thread,

Code: Select all

int pcamount=58;
int value=1192;

int amountofvalue=(value * pcamount)/100
There will be some rounding, due to the use of int values; use floats if you need more precision. I've deliberately used brackets to make sure that the multiplication happens first, to preserve information.

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...
Sarek
Captain Atari
Captain Atari
Posts: 374
Joined: Sat Nov 20, 2004 12:30 pm

Post by Sarek »

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.

Code: Select all

int pcamount=58; 
int value=1192; 

int amountofvalue=(value * pcamount +50)/100 
Just a suggestion. :)
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

Post by exxos »

thanks everyone for thier help, Knew it was simple but can never remember!

Chris
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

Post by exxos »

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)
User avatar
daeghnao
Captain Atari
Captain Atari
Posts: 479
Joined: Wed Oct 27, 2004 12:41 pm
Location: York, UK
Contact:

Post by daeghnao »

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)
Try:

Code: Select all

CoilCapacitance = ((Integer.toInt(form.toroid.value)) + CoilCapacitance)
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

Post by exxos »

xx = parseInt( form.toroid.value ); // convert to integer type
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
User avatar
daeghnao
Captain Atari
Captain Atari
Posts: 479
Joined: Wed Oct 27, 2004 12:41 pm
Location: York, UK
Contact:

Post by daeghnao »

Ahh, parseInt, that sounds about right.

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.
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

Post by exxos »

I'm not to great with Java TBH. Programming yes, java no. I think you are right about the string.

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 :-)
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

Post by exxos »

I have run into another problem :-(

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
User avatar
exxos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 4933
Joined: Fri Mar 28, 2003 8:36 pm
Location: England
Contact:

Post by exxos »

CoilPitch =( parseFloat(form.insulation.value)) + ( parseFloat(form.CoilPitch.value))


get there in then end!
User avatar
Desty
Atari God
Atari God
Posts: 1970
Joined: Thu Apr 01, 2004 2:36 pm
Location: 53 21N 6 18W
Contact:

Post by Desty »

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.

Code: Select all

int pcamount=58; 
int value=1192; 

int amountofvalue=(value * pcamount +50)/100 
Just a suggestion. :)
Looks scabby though; I'd just go with "Math.round(x * y / 100.0);"
tá'n poc ar buile!
Post Reply

Return to “Chat forum [ENG]”