Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to extract exact decimal part of any double type number in java

807589Jul 17 2008 — edited Jul 17 2008
{color:#ff0000}i want code to extract exact decimal part of any double type number in java....

{color}{color:#0000ff}for eg.
for 123.566

i want to get **.566**

please note i don't want .56600000000012 or even .5660000000000

{color}i tried this
import java.io.*;
class deci
{

public static void main(String[] args) throws IOException{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number");
double a=Double.parseDouble(obj.readLine());

int y=(int)a;
double x=y;
a=(double)a%(double)x;
System.out.println(a);
}

}
*{color:#ff6600}but it is giving me 0.5660000000000025{color}*
{color:#ff6600}for 123.566
{color}
{color:#ff6600}plz. help..........{color}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2008
Added on Jul 17 2008
8 comments
1,770 views