473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
11 22764
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
2,057 Expert 2GB
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
String decimal=txtAmount.getText();
int a = decimal.indexOf(".");
decimal=decimal.substring(a);
Jul 18 '09 #11
@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

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

Similar topics

1
by: Ricardo | last post by:
Hi. How can I -from an ASP page- get the decimal part of a number ? Thanks in advance for all your help.
1
by: Mark | last post by:
How can I get the decimal part of a number? For example, the number is 4.25. How do I get .25? Thanks! Mark
4
by: ai lian | last post by:
When I use printf to print a large double number, the result is not the same as the original input number. For example: double num=899999999999.894400; printf("%lf\n",num); The output is:...
2
by: Andy | last post by:
Hi I'm really stuck outputting a double number to the console with three decimal places if the furthest right value is a zero. I can coutput the number 4.546 as 4.546 but then if I output...
12
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString...
0
by: ichee | last post by:
Hi all, Is there any of u know how to randomize a double number in VB then use it as decimal type? I've try it many times, but unfortunately i'm still failed. I used this : private sub Init()...
3
by: bonymuscles | last post by:
Hi ppl... Hope ur havin a good day... here''s my doubt... I need to remove the decimal part of a number declared as DOUBLE. I can NOT change the type. I need to remove jus the decimal part......
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.