This is my first time making a GUI and we're making a stopwatch...I figured everything out except for how to make the time elapsed have one decimal place ex: 1.1 seconds

this is what I have so far

public void actionPerformed(ActionEvent ae){


		long endTime;
			display.setText("Start Button was Pressed");

		display.setText("Stop Button was Pressed");
		DecimalFormat df = new DecimalFormat("#.#");
		String i=df.format(((endTime-startTime)/1000));
		display.setText("Seconds : " + i);//milliseconds-->seconds


Any help is appreciated guys!

Maybe this will help you.
A long/long is long. long/int is long. double/int is double. double/long is double. long/double is double ... and so on.

Apply this to line 16 of your code.

Hope this helps.

String i=df.format(((endTime-startTime)/1000));

I assume startTime is a long.
(endTime-startTime) produces a long. Therefore (end-start)/1000 is integer division. Integers in java are a closed set under division.

What is, for example, 3452342L/1000?

You need to convince the machine to divide floating points, not integers.

EDIT: in other words, what keikkaishi said.

Edited 5 Years Ago by jon.kiparsky: n/a

This works for me:

public static void main(String[] args) {
        long time = System.currentTimeMillis();
        try {
        } catch (InterruptedException e) {
        long time2 = System.currentTimeMillis();
        NumberFormat nf = NumberFormat.getInstance();
        DecimalFormat df = null;
        if(nf instanceof DecimalFormat) {
            df = (DecimalFormat) nf;
            double seconds = (time2 - time)/1000d;
        } else {

Edited 3 Years Ago by Nick Evan: Fixed formatting

Thanks, I got it to work by just casting it to a double.

Thanks for your input guys!

This question has already been answered. Start a new discussion instead.