i want to print number after decimal point.Is there any way to do it e.g This is a decimal number 6.28 and i want to print .28.can any help me out with a simple example

The following code could be helpful.

public class Prinf{
	static double decimal(double d){
		int dd = (int)d;
		d -=dd;
		return d;
	public static void main(String args[]){
         double d = 6.328941;


If you are going to feed code to OPs, can you at least add comments to your code explaining the tricks you are doing?

The following code shows an idea on how to have only the figures after the decimal point are printed after the integer part is removed. Please rewording my comments if it is necessary.

public class Prinf{
	static double decimal(double d){ //a method to return the value after the decimal point
	int dd = (int)d; // cast into the int data type so that only the integer part is stored in dd
	d -=dd; // the value after the decimal point is finally left after the original value of the argument d minus the integer part dd.
	return d; // return the residual value, i.e. the value after the decimal point
	public static void main(String args[]){
         double d = 6.328941; // for example, we have a double variable of a value with 6 figures after the decimal point.
         System.out.printf("%8.6f\n",decimal(d));  // The 6 figures after the decimal point are printed after the integer part is removed.
commented: Good approach, I'd also have done this. :) +8

You can also do it another way, if you want to :)

First you make a String variable that holds the value of the double variable you are working with. You do this by using the method Double.toString() and the parameter you put in can either be the value directly, or the name of a double variable to which you have assigned the value you are working with.

Like this:

String number = Double.toString(6.28);

Or, like this:

double d = 6.28;
String number = Double.toString(d);

You then make an int variable that holds the value from the method indexOf() where you put in the char '.' so this method can return the indexposition of that char.

Like this:

int i = number.indexOf('.');

After that you finish by using the substring method in the String class and print it out.
The substring method wants the indexpositon of the first char in the substring that shall (in this case) be printed.

You can do it directly in the System.out method.
Like this:

System.out.println(number.substring(i) + "");

This is the same as if you had written:

String s = number.substring(i);
System.out.println(s + "");

The printed result will be .28

Good luck :)

commented: good solution, well explained. +1

and i come out without another way

import java.text.*;
class l{

	public static void main(String[] args){
   DecimalFormat c = new DecimalFormat("0.00");

		double d=6.28;
		int dd=(int)d;
		System.out.print("Decimal part ="+c.format(d-dd)+"\nintergal part = "+dd);


How is this different from what Tong posted?

tong used method and then call it main while i done it directly in main and i used class DecimalFormat to round the number so it make result much clean.

So it's Tong's code, and you just changed how it's called.

Okay, doing it your way, what happens if there are two numbers that you want to extract the decimal parts of?

jon.kiparsky check this out.

import javax.swing.*;
import java.text.*;

     class Demo
     public static void main (String[] args){

     DecimalFormat twoDigit= new DecimalFormat("0.00"); // rounds num to twodigits
     int j=0;
     double x=0.0;

     double[] I= new double[4];
     int[] DD= new int[4];

     while(j<=2) // loop to enter more than one decimal num

     String enter=JOptionPane.showInputDialog("enter num");
     double i=Double.parseDouble(enter);

     double z=i;
     int dd=(int)z; // taken z as int as temporaly storage to get integeral part of num
     DD[j]=dd; // array to store each number in it as loop circles

     I[j]=x;  // array to store decimal part in it as loop circles

       System.out.print("******Number before Decimal******\n");

       System.out.print("\nIntegeral part of number 1 = "+DD[0]+
       	                "\nIntegeral part of number 2 = "+DD[1]+
       	                "\nIntegeral part of number 3 = "+DD[2]+"\n");

       System.out.print("\n******Number after Decimal******"+"\n");

       System.out.print("\nDecimal part number 1 = "+twoDigit.format(I[0])+
                        "\nDecimal part number 2 = "+twoDigit.format(I[1])+
      	                "\nDecimal part number 3 = "+twoDigit.format(I[2]));

All I can say is, don't put that in your resume.

commented: Definitely. +14

You want to replace a four line method with 42 lines of code. In the process you take a good design decision - subbing out a discrete procedure to a method, so it can be maintained properly and called as needed - and revert to putting the same piece of code in a loop in the main body of the program. This is impossible to name, there's nothing formally indicating what goes in or what comes out, so we have to rely on inspection to determine that this does something. If we're lucky, there are comments, but if I see code like this, I delete the comments, because they'd confuse me more than the code does.

So we've got to look at this and work out what it's doing, which means we have to wonder why the graphics are folded into to the business logic - why on earth is the presentation mixed up with the calculation? Are we writing Basic now? And at that point, we've more or less decided that whatever it was this was supposed to do, the easiest thing to do is probably to just delete this mess from the repository, do a build, see what breaks, and fix that. Most likely, nothing much will break, because as soon as the lead on the project realizes what he's been saddled with, he's going to make sure the guy who can write stuff like this isn't allowed to touch anything that matters, so we can probably just delete it and walk away and save ourselves a lot of time.

This is bad code, my friend. This is the wrong thing to do, and the fact that you offer - apparently in all seriousness - as an alternative to a nice, tight piece of code suggests to me that you really don't know what you're doing. If I can determine that from looking at this piece of code - and not even from reading it, from looking at it - then this is not a piece of code you want to show to anyone as an example of what you can do.

The punchline to all of this? My job title: technical writer. I'm a writer of English by trade, and of Java by hobby only, and this forty lines caused me despair so deep I nearly became an existentialist. You, yes you, you nearly caused me to spend the rest of my days wearing black, smoking horrible cigarettes in little bistros on the left bank, fifty years ago. I don't know how I would have managed it, but I would have, to get away from that putrescent mess you have inflicted on us.

Please, please stop. You're killing me with your awful suck. When you see something good, learn from it. Don't fix away the good bits.

There. Is that a little more clear?

ithink you dont have any info about use of array and if this seems you awful than leave programming and look a office job for your self where you can just sit and eat.isn't easily job haan.i am not here to explain everything to you just use your brain and even if dont haan and just use this forum to paste this hell statement than take mine suggestion don't do this programming go and sit in a office where you can not use your this hammty dammty

commented: The code was awful. Stop having a tantrum. -3

this not a place for those uneducated person who take java as just a time pass join an english poetry forum better for you.
OHH God let these people out from here