vk01 Posted November 2, 2007 Share Posted November 2, 2007 Hi, I have calculations in the view which is used in the report. Now i would like to create a sum calculation on this field. The field is defined as "java.lang.string". how to create a sum cal on this? I tried changing the field to a BigDecimal and then tried a sum, still results in an error. the result looks like "00:01:31" - meaning like one minute 31 seconds. all the results are returned in this format. thanks for your time. Link to comment Share on other sites More sharing options...
vinays84 Posted November 2, 2007 Share Posted November 2, 2007 How are you trying to sum?If your fields are all n the format "00:01:31", would "00:01:31" + "00:01:31" = "00:03:02"? Please better define what your are trying to do. Link to comment Share on other sites More sharing options...
vk01 Posted November 2, 2007 Author Share Posted November 2, 2007 that is right - it would have to add up appropriately, just like in your example. Link to comment Share on other sites More sharing options...
vinays84 Posted November 3, 2007 Share Posted November 3, 2007 I would suggest converting each string field (lets call it timeStr) to a decimal value representing seconds. Thus the variable (lets call it timeSum) would always be equal to the sum shown in seconds and would be of type Integer (or BigDecimal). Your increment expression would be:Code:new Integer (3600*Integer.parseInt($F{timeStr}.split(":"«»)[0])+60*Integer.parseInt($F{timeStr}.split(":"«»)[1])+Integer.parseInt($F{timeStr}.split(":"«»)[2])) Your initial value expression would be:Code:[code]new Integer(0) And to get the variable back into String format when you wish you would use:Code:[code]($V{timeSum}.intValue()/3600)+":"+(($V{timeSum}.intValue()%3600)/60)+":"+($V{timeSum}.intValue()%60) Of course, this depends on all $F{timeStr} being in the expected format "hh:mm:ss". If not, error check'll need to be added. Link to comment Share on other sites More sharing options...
vk01 Posted November 5, 2007 Author Share Posted November 5, 2007 Hi, I have defined it a double, when i do try this calculation========(new java.lang.Double( ($V{TOTAL_PRETRIP}.doubleValue()) / 3600)) + ":" +(new java.lang.Double( (($V{TOTAL_PRETRIP}.doubleValue()) % 3600) / 60)) + ":" +(new java.lang.Double( (($V{TOTAL_PRETRIP}.doubleValue()) %60)))===it comes up with something like - 3152 seconds - 0.875555:52.5355555:32.0 how can we get something like - 00:52:32 thanks so much for your time. Link to comment Share on other sites More sharing options...
vinays84 Posted November 5, 2007 Share Posted November 5, 2007 Call intValue() instead of doubleValue(). Link to comment Share on other sites More sharing options...
vk01 Posted November 5, 2007 Author Share Posted November 5, 2007 AWESOME! Thanks, that really helped. One last thing - could this formatted for two digits? - it retrieves a value like - 1:9:38 for 4178 seconds value. need it show up like - 01:09:38 - Thanks again! Link to comment Share on other sites More sharing options...
vinays84 Posted November 5, 2007 Share Posted November 5, 2007 A good point. The direct approach would be to add extra conditionals to each line to check if the number < 10 and add a "0" accordingly. This will more than double the code and make it really messy, though, which I don't like. I got a nifty method to avoid this. Because we know each value will be under 100, we can add 100, convert to a String and then take the substring, starting at the 2nd character.Try:Code:Integer.toString(100+($V{TOTAL_PRETRIP}.intValue() / 3600)).substring(1) + ":"+Integer.toString(100+(($V{TOTAL_PRETRIP}.intValue() % 3600) / 60)).substring(1) + ":" +Integer.toString(100+($V{TOTAL_PRETRIP}.intValue() %60)).substring(1) If you think your hour amount will ever exceed 100, however, you'll have to use the conditional in the first statement: Code:[code](($V{TOTAL_PRETRIP}.intValue() / 3600)<10)?"0":"" +($V{TOTAL_PRETRIP}.intValue() / 3600) + ":" +Integer.toString(100+(($V{TOTAL_PRETRIP}.intValue() % 3600) / 60)).substring(1) + ":" + Integer.toString(100+($V{TOTAL_PRETRIP}.intValue() %60)).substring(1) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now