Connecting Tech Pros Worldwide Help | Site Map

problem in getting the decimal part of a double number in java

Member
 
Join Date: Feb 2007
Posts: 58
#1: Mar 9 '07
Ive got a double number.

double n=47758.35;
Inorder to get the decimal part of this number i did as follows

long decPart = (long) ((n-Math.floor(n))*100);

in most cases i get the correct decimal part but in this case the decPart becomes"34" instead of "35"

how can i get the exact decimal part

Please Help
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Mar 9 '07

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by shiniskumar

Ive got a double number.

double n=47758.35;
Inorder to get the decimal part of this number i did as follows

long decPart = (long) ((n-Math.floor(n))*100);

in most cases i get the correct decimal part but in this case the decPart becomes"34" instead of "35"

how can i get the exact decimal part

Please Help

You can split it up as string

Expand|Select|Wrap|Line Numbers
  1.  String s = ""+n; 
  2. String[] nums = s.split(".");
  3.  
Now the decimal part is
Expand|Select|Wrap|Line Numbers
  1.  int decimal = Integer.parseInt(nums[1]);
Member
 
Join Date: Feb 2007
Posts: 58
#3: Mar 9 '07

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by r035198x

You can split it up as string

Expand|Select|Wrap|Line Numbers
  1.  String s = ""+n; 
  2. String[] nums = s.split(".");
  3.  
Now the decimal part is
Expand|Select|Wrap|Line Numbers
  1.  int decimal = Integer.parseInt(nums[1]);

Im gettinf nums as empty string after spilting
ie if n=2345.65 then s="2345.65"
but nums becomes []
so cant take nums[1]......
exception arrayindexoutofbound
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Mar 9 '07

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by shiniskumar

Im gettinf nums as empty string after spilting
ie if n=2345.65 then s="2345.65"
but nums becomes []
so cant take nums[1]......
exception arrayindexoutofbound

Use

Expand|Select|Wrap|Line Numbers
  1.  String[] nums = s.split("\\.");
Member
 
Join Date: Feb 2007
Posts: 58
#5: Mar 9 '07

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by r035198x

Use

Expand|Select|Wrap|Line Numbers
  1.  String[] nums = s.split("\\.");

Thanks, its working

Can u tell me the difference between those two types of split.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Mar 9 '07

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by shiniskumar

Thanks, its working

Can u tell me the difference between those two types of split.

The . is a special character so you have to use \\ to match it in the regular expression.
Newbie
 
Join Date: Nov 2008
Posts: 4
#7: Nov 10 '08

re: problem in getting the decimal part of a double number in java


Have you considered what happens for large double values?

For example:

double aNum = 444444444.56
System.out.println("my number is: " + aNum);

the result is:

4.4444444456E8


You can get around this with the DecimalFromat class with a tradeoff in performance.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: Nov 11 '08

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by r035198x

You can split it up as string

Expand|Select|Wrap|Line Numbers
  1.  String s = ""+n; 
  2. String[] nums = s.split(".");
  3.  
Now the decimal part is
Expand|Select|Wrap|Line Numbers
  1.  int decimal = Integer.parseInt(nums[1]);

Ha, priceless.
Newbie
 
Join Date: Nov 2008
Posts: 4
#9: Nov 12 '08

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by r035198x

Ha, priceless.

If 'n' is a large double it will be converted to scientific notation when printed as a string.
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#10: Nov 12 '08

re: problem in getting the decimal part of a double number in java


It may be a rounding error due to returning a double. You could also try using round.

long decPart = Math.round((n-Math.floor(n))*100);
CodeWeaver's Avatar
Newbie
 
Join Date: Jul 2009
Location: Pune,India
Posts: 2
#11: Jul 18 '09

re: problem in getting the decimal part of a double number in java


String decimal=txtAmount.getText();
int a = decimal.indexOf(".");
decimal=decimal.substring(a);
CodeWeaver's Avatar
Newbie
 
Join Date: Jul 2009
Location: Pune,India
Posts: 2
#12: Jul 19 '09

re: problem in getting the decimal part of a double number in java


Quote:

Originally Posted by shiniskumar View Post

Ive got a double number.

double n=47758.35;
Inorder to get the decimal part of this number i did as follows

long decPart = (long) ((n-Math.floor(n))*100);

in most cases i get the correct decimal part but in this case the decPart becomes"34" instead of "35"

how can i get the exact decimal part

Please Help

As you mentioned it has problem when asked for 35 it gives 34 sort off

and also the splitting method gives problem like if we have 3.04 so the output will be 4 and if its 3.40 even now the output will be 4
the method i mentioned above gives correct output like 04 or 40
Reply