Newbie Question

Discussion in 'Java' started by Obi Wan Shinobi, Jun 13, 2004.

  1. Hello there,

    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?

    Many thanks in advance.
    Obi Wan Shinobi, Jun 13, 2004
    #1
    1. Advertising

  2. Obi Wan Shinobi

    Roedy Green Guest

    On 13 Jun 2004 07:32:57 -0700, (Obi Wan Shinobi)
    wrote or quoted :

    >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?


    see http://mindprod.com/jgloss/round.html
    for how to extract the integer part. then subtract.

    Multiply it first if you really mean you want an int result. There is
    also floatToIntBits.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 13, 2004
    #2
    1. Advertising

  3. Obi Wan Shinobi

    Jim Sculley Guest

    Obi Wan Shinobi wrote:
    > Hello there,
    >
    > 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?


    Uhm, you can't. Short and int only accept integers, and the fractional
    part of a double or float is, of course, not an integer.

    Unless you mean that you wish to take a number such as 1.25, and store
    '25'. In that case, String.valueOf(), Short.parseShort() and
    Integer.parseInt() may be of assistance.

    Jim S.


    --
    Remove my extraneous mandibular appendages to reply via email.
    Jim Sculley, Jun 13, 2004
    #3
  4. Obi Wan Shinobi

    Oscar kind Guest

    Obi Wan Shinobi <> 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.longValue()));
    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
    Oscar kind, Jun 13, 2004
    #4
  5. Oscar kind wrote:

    > 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);


    Oscar, you DID mean to write "BigDecimal number = new BigDecimal(3.05);"
    right? and not the "BigDecimal number = new DigBecimal(3.05);" that you
    mistakenly wrote??!!
    Steve R. Burrus, Jun 13, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry C.
    Replies:
    8
    Views:
    214
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    131
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    113
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    102
  5. Miki Tebeka
    Replies:
    0
    Views:
    76
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page