1

I thought by casting the value to float it would show the seconds to the tenths place.

long startTime = System.currentTimeMillis();

    while (RUNNING) {
        track.repaint();
        // varying the x position movement
        horse.setX(xpos += (int) (Math.random() * 10 + 0));
        // Sleeping the thread
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (xpos >= FINISH_LINE) {              
            RUNNING = false;
            long endTime = System.currentTimeMillis();
            JOptionPane.showMessageDialog(new JFrame(), id + " Won and took " +(float)(startTime - endTime)/1000*-1.0+ " seconds");             
        }

    }

instead, it is giving me way too much precision. I would like to cut it off a bit.

LeatherFace
  • 478
  • 2
  • 11

1 Answers1

1

It's easy! Use DecimalFormat:

JOptionPane.showMessageDialog(new JFrame(), id + " Won and took " + new DecimalFormat(".##").format((float)(startTime - endTime)/1000*-1.0)+ " seconds");
Azar
  • 1,086
  • 12
  • 27