By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,199 Members | 1,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,199 IT Pros & Developers. It's quick & easy.

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

P: 58
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
Mar 9 '07 #1
Share this Question
Share on Google+
11 Replies


10K+
P: 13,264
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]);
Mar 9 '07 #2

P: 58
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
Mar 9 '07 #3

10K+
P: 13,264
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("\\.");
Mar 9 '07 #4

P: 58
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.
Mar 9 '07 #5

10K+
P: 13,264
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.
Mar 9 '07 #6

P: 4
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.
Nov 10 '08 #7

10K+
P: 13,264
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.
Nov 11 '08 #8

P: 4
Ha, priceless.
If 'n' is a large double it will be converted to scientific notation when printed as a string.
Nov 12 '08 #9

jkmyoung
Expert 100+
P: 2,057
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);
Nov 12 '08 #10

CodeWeaver
P: 2
String decimal=txtAmount.getText();
int a = decimal.indexOf(".");
decimal=decimal.substring(a);
Jul 18 '09 #11

CodeWeaver
P: 2
@shiniskumar
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
Jul 19 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.