# Java: Get part before and after decimal point

How to get the 2 parts from a double value: 3.141592654 ???

part1 = 3
part2 = 141592654
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
conver the dobule to String and then do a indexOf ..

something like..

Double d = new Double(3.1415......);
String sd = d.toString();
int idx = sd.indexOf(".");
String part1 = sd.substring(0,idx);
String part2 = sd.subString(idx);
Commented:
Double d = new Double(3.141592654);
long part1 = d.longValue();
long part2 = d.toString().substring(d.toString().indexOf('.'), d.toString().length());
Sr. Software engineerCommented:
What are part1 & part2?
String's, doubl's, long's??
Commented:
double d = 3.141592654;

long integerPart = (long)d;
double decimalPart =  d - (double)integerPart;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
for strings:
String[] parts = new Double(d).toString().split("\\.");
String part1 = parts[0];
String part2 = parts[1];

for longs:
String[] parts = new Double(d).toString().split("\\.");
Long part1 = Long.parseLong(parts[0]);
Long part2 = Long.parseLong(parts[1]);
Sr. Software engineerCommented:
for doubles:
String[] parts = new Double(d).toString().split("\\.");
double part1 = Double.parseDouble(parts[0]);
double part2 = Double.parseDouble(parts[1]);

;°)
Commented:
dberner9's code will work for JDK1.4 & above but its if you are using 1.4 then its the right way of doing it
Author Commented:
will test on monday and come back to you. thanks so far :-)
Commented:
>>double d = 3.141592654;
>>long integerPart = (long)d;
>>double decimalPart =  d - (double)integerPart;

intelligence solution, but
part2 (decimalPart) will contains ( . ) /*dot*/ at the front ..
Principal TechnologistCommented:
>> part2 (decimalPart) will contains ( . ) /*dot*/ at the front ..

That is what is required? I guess you can also use:

long integerPart = ( long ) d ;
double decimalPart = d - integerPart ; // no need to type-cast
Sr. Software engineerCommented:
thanks
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.