Obi Wan Shinobi <(E-Mail Removed)> wrote:

> I've just started learning Java. What I would like to know is if I

> have a variable of type double or float, how can I extract the

> fractional portion of that number and place it into a short or int

> variable?
You don't, as 0.2 is greater than 0.15, but 2 is smaller than 15.

The best way to do such a thing is to use fixed point numbers.

A code snippet to illustrate:

BigDecimal number = new DigBecimal(3.05);

number = number.setScale(3, BigDecimal.ROUND_HALF_UP);

number = number.substract(BigDecimal.valueOf(number.longVal ue()));

long fraction = number.unscaledValue().longValue();

The variable fraction now contains 50. This is the fractional part times

1000, because the scale was set to 3 (10^3 = 1000).

kind regards,

Oscar

--

Oscar Kind

http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2