LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-06-2003, 11:07 AM   #1
spyghost
Member
 
Registered: Jun 2003
Distribution: Redhat
Posts: 245

Rep: Reputation: 30
Question floating point numbers in java


hi,

i know this is a very stupid newbie question, but i would like to ask it... (sorry for my dumbness)

how would i extract the decimal part of a floating point and how would i extract the whole number part from a floating point?

ex: 735.15 - i would like to assign 735 to one variable and 0.15 to another.

thanks
 
Old 09-06-2003, 12:35 PM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
This is a quick put together which doesn't do all the validation it really should but for now..


Code:

public class test
{
 public static void main(String args[])
 {

  double number=735.15;
  int intPart=0; 
  int decimalPoint=0;
  double decimalPart=0;

  StringBuffer stringNum=new StringBuffer(Double.toString(number));

  decimalPoint=stringNum.indexOf(".");

  intPart=Integer.parseInt(stringNum.substring(0,decimalPoint));
  
  decimalPart=Double.parseDouble(stringNum.substring(decimalPoint,stringNum.length()));

  
  System.out.println("Whole Number: " + intPart + "\n" + "Decimal Part: " + decimalPart);


 }
}
 
Old 09-06-2003, 01:13 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
i dont know java so this is probably wrong but if you can typecast then i would do this
Code:
float num=12.345;
float fPart;
int iPart;

iPart = (int)num;
fPart = num - (float)iPart;
 
Old 09-06-2003, 01:34 PM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I was think of that but thought the casting to int might end up rounding up the number in some cases.
 
Old 09-06-2003, 01:53 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
as i said i dont know java but in c/c++ casting to an int just takes the integer part it doesnt round.
 
Old 09-06-2003, 02:07 PM   #6
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Yeah, it does, once you reach the limit, although admittedly it is quite a few decimal places, 0.9999999999999999 or whatever amount, I guess it just depends on what they're doing or whatever size of numbers they'll be dealing with, either way, they've got two suggestions now so....
 
Old 09-06-2003, 05:53 PM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
ive just tried myself and have written the following to test if typecasting rounds because i was pretty sure it didnt, anyway i wrote the following program
Code:
import java.util.*;
import java.lang.Math.*;

class rounding
{
    public static void main(String[] args)
    {
	for(int p=0;p<25;p++) {
	    double num = (double)10 - Math.pow(10, -p);
	    int iPart = (int)num;
	    double fPart = num - (double)iPart;

	    System.out.println("num: "+num+"\tip: "+iPart+"\tfp: "+fPart);
	}
    }
}
and got this as my output
Code:
num: 9.0        ip: 9   fp: 0.0
num: 9.9        ip: 9   fp: 0.9000000000000004
num: 9.99       ip: 9   fp: 0.9900000000000002
num: 9.999      ip: 9   fp: 0.9990000000000006
num: 9.9999     ip: 9   fp: 0.9999000000000002
num: 9.99999    ip: 9   fp: 0.9999900000000004
num: 9.999999   ip: 9   fp: 0.9999990000000007
num: 9.9999999  ip: 9   fp: 0.9999999000000006
num: 9.99999999 ip: 9   fp: 0.9999999899999992
num: 9.999999999        ip: 9   fp: 0.9999999989999999
num: 9.9999999999       ip: 9   fp: 0.9999999999
num: 9.99999999999      ip: 9   fp: 0.9999999999900009
num: 9.999999999999     ip: 9   fp: 0.9999999999989999
num: 9.9999999999999    ip: 9   fp: 0.9999999999999005
num: 9.99999999999999   ip: 9   fp: 0.9999999999999893
num: 9.999999999999998  ip: 9   fp: 0.9999999999999982
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
num: 10.0       ip: 10  fp: 0.0
as you can see iPart is always the integer part of num, it doesnt round, however 9.99999... does eventually round to 10.0, i think this is what led you to your conclusion that typecasting to an int rounds but as you can see it doesnt.
 
Old 09-06-2003, 06:42 PM   #8
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I'm the one that needs the help now

So what's happening is when the type reaches it's precision limit for it's size it's been rounded up because of that not because of casting?

Goes to show appearances can be deceptive and looking again my overblown way would have suffered from the exact thing it was trying to prevent because it parsed back into a number.

Another little bit of insight gleemed from the LQ boards
 
Old 09-06-2003, 07:21 PM   #9
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
Quote:
I'm the one that needs the help now
everbody needs help sometimes, took me 3 minutes to write that program then over an hour to compile/run it

Quote:
So what's happening is when the type reaches it's precision limit for it's size it's been rounded up because of that not because of casting?
yep

Quote:
looking again my overblown way would have suffered from the exact thing it was trying to prevent because it parsed back into a number.
your method would suffer like mine but it would be due to the fact that number would be rounded up at the point you set it, which is obviosly before you convert it into a string. however your method is slightly more accurate than mine because i subtract and hence introduce numerical errors, you method has no loss of accuracy at all.

Quote:
Another little bit of insight gleemed from the LQ boards
thats why we're all here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating Point in kernel 2.4.28 lucs Slackware 1 10-19-2005 09:33 AM
Floating point exception hemk76 Linux - Software 1 05-07-2005 12:49 AM
floating point multiplication irfanhab Programming 0 12-23-2004 11:13 PM
modulo of 2 floating point numbers? jenna_h Programming 6 09-24-2004 10:39 AM
managing floating point vince_2x Linux - General 2 09-21-2004 10:05 PM


All times are GMT -5. The time now is 08:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration