Re: Rounding

Splash Forums PrettyTime Users Rounding Re: Rounding

#20280

Wachunga
Participant

Sorry, that was a typo on my part. After copying and pasting the test, I decided to make it “more clear” for the purposes of this post but ended up botching it.

Here’s the update test:

@test

public void formatDifferencePast() {

final DateTime now = new DateTime(2010, 1, 8, 0, 0, 0, 0);

final DateTime almostFourHoursAgo = now.minusHours(3).minusMinutes(58);

final DateTime fourHoursAgo = now.minusHours(4);

final DateTime justOverFourHoursAgo = now.minusHours(4).minusMinutes(10);

assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, almostFourHoursAgo));

assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, fourHoursAgo));

assertEquals(“4 hours ago”, DateTimes.formatDifferenceApproximately(now, justOverFourHoursAgo));

}

By default (without tweaking roundingTolerance), assertion 1 fails. I get “3 hours ago” for a time that’s slightly less than 4 hours ago.

If I set roundingTolerance to 50 (or anything below high 90s), same problem.

If I set roundingTolerance to 99, assertion 1 is fine but 3 fails. I get “5 hours ago” for a time that’s slightly more than 4 hours ago.

Is there a way to round to the nearest hour? It’s quite important for my use case that >= .5 rounds up and < .5 rounds down.