Here is another way to write it that is similar to Shabbir's ^ but I think is easier to read. I actually wanted to round it; if it showed .349, I wanted to see .35 - but if you don't want that then you can use Math.floor in place of my Math.round:
public static double round(double time){
time = Math.round(100*time);
return time /= 100;
}
Here is my complete code: I am working on a program to find 3 random 1 min time intervals in a given time segment, for a research project im working on.
import java.lang.*;
import java.util.*;
class time{
public static void main (String[] args){
int time = Integer.parseInt(args[0]);
System.out.println("time is: "+time);
//randomly select one eligible start time among many
//the time must not start at the end, a full min is needed from times given
time = time - 1;
double start1 = Math.random()*time;
System.out.println(start1);
start1 = round(start1);
System.out.println(start1);
}
public static double round(double time){
time = Math.round(100*time);
return time /= 100;
}
}
to run it, with current output:
[bharthur@unix2 edu]$ java time 12
time is: 12
10.757832858914
10.76
[bharthur@unix2 edu]$ java time 12
time is: 12
0.043720864837211715
0.04