Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Newbie Question

Reply
Thread Tools

Newbie Question

 
 
Obi Wan Shinobi
Guest
Posts: n/a
 
      06-13-2004
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.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-13-2004
On 13 Jun 2004 07:32:57 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
 
Jim Sculley
Guest
Posts: n/a
 
      06-13-2004
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.
 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      06-13-2004
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
 
Reply With Quote
 
Steve R. Burrus
Guest
Posts: n/a
 
      06-13-2004
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??!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments