Jump to content

sum up a string value


vk01

Recommended Posts

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

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...