How to round calculated script runner values for numeric fields.

We have a working script to calculate a series of 13 percentages that are used to populate 13 numeric fields on a transition post function; however, we are having difficulties in rounding the values to achieve an integer resultant.  The working script follows:



import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

Issue issue = issue;
def fieldList = [
[Numer:'LIFE Enrolled Lives',Denom:'LIFE Eligible Lives',Reslt:'LIFE Participation Percentage'],
[Numer:'VTL Enrolled Lives',Denom:'VTL Eligible Lives',Reslt:'VTL Participation Percentage'],
[Numer:'STD Enrolled Lives',Denom:'STD Eligible Lives',Reslt:'STD Participation Percentage'],
[Numer:'PCVSTD Enrolled Lives',Denom:'PCVSTD Eligible Lives',Reslt:'PCVSTD Participation Percentage'],
[Numer:'LTD Enrolled Lives',Denom:'LTD Eligible Lives',Reslt:'LTD Participation Percentage'],
[Numer:'VLTD Enrolled Lives',Denom:'VLTD Eligible Lives',Reslt:'VLTD Participation Percentage'],
[Numer:'DENTAL Enrolled Lives',Denom:'DENTAL Eligible Lives',Reslt:'DENTAL Participation Percentage'],
[Numer:'VDEN Enrolled Lives',Denom:'VDEN Eligible Lives',Reslt:'VDEN Participation Percentage'],
[Numer:'CI Enrolled Lives',Denom:'CI Eligible Lives',Reslt:'CI Participation Percentage'],
[Numer:'VCI Enrolled Lives',Denom:'VCI Eligible Lives',Reslt:'VCI Participation Percentage'],
[Numer:'ACC Enrolled Lives',Denom:'ACC Eligible Lives',Reslt:'ACC Participation Percentage'],
[Numer:'VACC Enrolled Lives',Denom:'VACC Eligible Lives',Reslt:'VACC Participation Percentage'],
[Numer:'Other Enrolled Lives',Denom:'Other Eligible Lives',Reslt:'Other Participation Percentage']

fieldList.each { val ->
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtNumerator = customFieldManager.getCustomFieldObjects(issue).find

{ == val.Numer}

def tgtDenominator = customFieldManager.getCustomFieldObjects(issue).find

{ == val.Denom}

def tgtResult = customFieldManager.getCustomFieldObjects(issue).find

{ == val.Reslt}

def numerator = issue.getCustomFieldValue(tgtNumerator)
def denominator = issue.getCustomFieldValue(tgtDenominator)
def result = issue.getCustomFieldValue(tgtResult)

def changeHolder = new DefaultIssueChangeHolder() //Do not need to recreate

if ((numerator && numerator > -1) && (denominator && denominator > 0 ))

{ tgtResult.updateValue(null, issue, new ModifiedValue(result, (numerator/denominator)*100),changeHolder) }


{ tgtResult.updateValue(null, issue, new ModifiedValue(result, null),changeHolder) }




Our attempts to use Math.round for the rounding are successful when using the Groovy Web Console but have not been successful when using your ScriptRunner plugin.  See sample error message below:


2016-03-23 08:04:00,376 http-bio-8530-exec-9 ERROR req86806 484x68791x1 q47b5h, /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: VERA-15, actionId: 181, file: <inline script>
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
	at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(
	at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(
	at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(
	at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(
	at com.atlassian.jira.issue.fields.OrderableField$ Source)
	at Script97$_run_closure1.doCall(Script97.groovy:40)


Please provide needed guidance/tips.

Many thanks for your efforts.



1 answer

1 accepted

0 votes
Accepted answer

Could you format the code using the {code} macro, its difficult to read.

It looks like your custom field is returning a long where Math.round is expecting a double. You need to convert it to a double like so if you want to use Math.round.

Long l = new Long(15552451L)
double d = l.doubleValue()



Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

441 views 8 26
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you