aspose file tools
The moose likes Java in General and the fly likes rounding  a fraction+time Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "rounding  a fraction+time" Watch "rounding  a fraction+time" New topic
Author

rounding a fraction+time

meera jan
Greenhorn

Joined: Aug 30, 2005
Posts: 21
hi all,
i don't know..whether this is the right forum tp post this doubt..
First of all my doubt is,how to find the difference between 2 time values in java.
i have 2 values
time1= "9.20"
time2="18.00"
what i want is the difference 8.40.how to get it?
also the second doubt how to round a fraction to 2 decimal points...
8.6667899 to 8.67.
pls help
thanx in advance
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

"googly man"

Pleave review our naming policy and adjust your display name accordingly.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"meera",
Thank you for changing your display name, but it still not correct.

We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Thanks,
Dave
[ October 10, 2005: Message edited by: David O'Meara ]
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
-------------------------------------------------------------
the second doubt how to round a fraction to 2 decimal points...
8.6667899 to 8.67.
-------------------------------------------------------------

U can use NumberFormat Class. check the API for the method, i think its setMaximumFractionDigit(2). check the api for the method explanation.

----------------------------------------------------
the difference between 2 time values in java
----------------------------------------------------

U can convert the time to milliseconds, find the difference as its a integer and then convert back to hours.
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 58
1. Construct two GregorianCalendar objects gc1 and gc2
2. Calculate Elapsed Seconds
long getElapsedSeconds(gc1, gc2) {
Date d1 = gc1.getTime();
Date d2 = gc2.getTime();
long l1 = d1.getTime();
long l2 = d2.getTime();
long difference = Math.abs(l2 - l1);
return difference / 1000;
}
3.
long seconds = getElapsedSeconds(gc1, gc2)
int hours = (int)seconds / 3600;
seconds = seconds - (hours * 3600);
int minutes = (int) seconds / 60;
int ss = (int)seconds - (minutes * 60);
Pratik Lohia
Ranch Hand

Joined: May 05, 2005
Posts: 88
Use this ro round up to tow decimals.

public static double roundDoubleValue(double val2Round) {
/*
* Method to round off a recurring double value to a two decimal precision.
*
*/
BigDecimal bd = new BigDecimal(val2Round);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
// now to remove zero after decimal
return bd.doubleValue();
} // end of method roundDoubleValue
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: rounding a fraction+time
 
Similar Threads
doubt
doubt on HashMap
Doubt on doubt
doubt in Math.round function.
Doubt in InnerClass