Nick
Nick

Reputation: 135

Java delete numbers behind comma

I want to calculate Days, Hours etc.

I want to make it like this: 184 Seconds / 60 = 3,0666666666667 Means 3 Minutes. 0,666666666667 * 60 = 4

So 184 Seconds are 3 Min. und 4 Seconds. Now i dont know how to bring this into Java. I need a function to seperate the Pre-Comma Value from the After-Comma Value.

It's just a simple example. I want to do this with years,weeks,days and so on

Upvotes: 0

Views: 198

Answers (2)

Pshemo
Pshemo

Reputation: 124235

It seems that you are looking for modulo (reminder) operator %. Also there is no "after comma value" in integers words so 184 / 60 = 3 not 3.06666.

int time = 184;
int minutes = time / 60;
int seconds = time % 60;
System.out.println(minutes + " minutes : " + seconds + " seconds");

Output: 3 minutes : 4 seconds


You can also use Period from JodaTime library.

int time = 184;
Period period = new Period(time * 1000);//in milliseconds 

System.out.printf("%d minutes, %d seconds%n", period.getMinutes(),
        period.getSeconds());

which will print 3 minutes, 4 seconds.

Upvotes: 1

Just use %, /, and a little math:

int totalSeconds = 184;
int minutes = totalSeconds/60; //will be 3 minutes
int seconds = totalSeconds%60; // will be 4 seconds

Upvotes: 0

Related Questions