newbe seperate number into two components

S

Steve

Hi, I need to be able to seperate a number into two components, its decimal
and non decimal component.

For example:
Long Price = 108100
ModifiedPrice= Price/1000 (ie. 108.100)

I need to store these value in two different varibales
numbercomponent = ? (108)
decimalcomponent= ? (.100)

I hope that this makes sense.
Thank you
 
M

Mike Smith

Use the MOD operator which is %

numbercomponent = modifiedPrice/1000;
decimalcomponent= modifiedPrice%1000;

Mike
 
S

Steve

Thanks Mike.

Works like a charm. How in Java do I divide by a decimal?

I have tried the following code but it does not like the .32

MNC=numberComponent/.32

OR

MNC=numberComponent/(32/100)

Thanks
 
O

Oscar kind

Steve said:
Hi, I need to be able to seperate a number into two components, its decimal
and non decimal component.

Have a look at the method Math.round(double).
For example:
Long Price = 108100
ModifiedPrice= Price/1000 (ie. 108.100)

double price = 108.10;
I need to store these value in two different varibales
numbercomponent = ? (108)

long numberComponent = Math.round(price); // The integer part
decimalcomponent= ? (.100)

double decimalComponent = price - numberComponent; // The fractional part


Oscar
 
M

Michael Rauscher

Hi Steve,
Thanks Mike.

Works like a charm. How in Java do I divide by a decimal?

I have tried the following code but it does not like the .32

MNC=numberComponent/.32

I assume that MNC is an int or a long.

The result of the calculation is of type double because .32 is of type
double. Therefore you assign a double value to an integer variable and
the compiler claims that this would result in a possible loss of
precision. You have to tell the compiler that you're interested only in
the integer value of your calculation:

long MNC;
MNC = (long)(numberComponent/.32);

or
int MNC
MNC = (int)(numberComponent/.32);

respectively.

OR

MNC=numberComponent/(32/100)

This should compile, but throw an ArithmeticException (divide by zero).
Have a look at the numbers within the parenthesis. They are both integer
numbers. Therefore the result is an integer number, that is:

32/100 == (int)(32.0/100.0) == (int)(0.32) == 0

Bye
Michael
 
T

Thomas Schodt

Steve said:
How in Java do I divide by a decimal?

I have tried the following code but it does not like the .32

MNC=numberComponent/.32

Floating point arithmetic.
You can just cast it back to an int.

MNC = (int)(numberComponent/.32);
OR

MNC=numberComponent/(32/100)

Division by zero.
Java uses integer arithmetic unless some of the values are floating
point values. [Or long arithmetic if a value is long.]

As you told it to evaluate 32/100 first, giving zero...


You can just change the order.

MNC = numberComponent * 100 / 32;

or if you want rounding

MNC = (numberComponent * 100 * 2 + 32 ) / 2 / 32;
 
S

steve

Thanks Mike.

Works like a charm. How in Java do I divide by a decimal?

I have tried the following code but it does not like the .32

MNC=numberComponent/.32

OR

MNC=numberComponent/(32/100)

Thanks

You need a book on basic java, otherwise you are going to have to keep coming
back to the newsgroup on every command, try the o'reilly book "learning
Java"
it covers all the questions you already asked, why it works that way, and has
good examples.

steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top