Problem with number rounding down rather than showing decimal points

Current system has the following code to compute the average of 5 different number. All have a default value of 0. For example:4+7+4+7+4=26 / 5 =5.2 but system shows 5. Please help how to change my code:

 

<!-- @@Formula: 
double val = 0;
double weight = 0; 
val = ( 
(issue.get("customfield_12900") != null ? Integer.parseInt(issue.get("customfield_12900").toString()) : 0) + 
(issue.get("customfield_12901") != null ? Integer.parseInt(issue.get("customfield_12901").toString()) : 0) +
(issue.get("customfield_12902") != null ? Integer.parseInt(issue.get("customfield_12902").toString()) : 0) +
(issue.get("customfield_12903") != null ? Integer.parseInt(issue.get("customfield_12903").toString()) : 0) +
(issue.get("customfield_12904") != null ? Integer.parseInt(issue.get("customfield_12904").toString()) : 0)
) / 5;
return val;
-->
<!-- @@Format: 
numberTool.format("#0.00",value);
-->

3 answers

1 accepted

Foremost, this is Misc Custom Field's syntax but you marked JIRA Cloud... I don't believe that Misc Custom Fields is available in JIRA Cloud.

Second, you are using "Integer.parseInt..." Integers by design do NOT handle decimal points. Please modify your code to use a proper type instead of discarding decimals. If you'd like help with your script, please plainly state your use-case.

Further to the comment from @Steven Behnke He is correct that the values you are retrieving are all integers hence why when you do the calculation it returns an integer. I believe although I am not an expert in the formats for Misc Custom Fields syntax that the syntax you should be using is

 

<!-- @@Formula: 
double val = 0;
double weight = 0; 
val = ( 
(issue.get("customfield_12900") != null ? float.parseInt(issue.get("customfield_12900").toString()) : 0) + 
(issue.get("customfield_12901") != null ? float.parseInt(issue.get("customfield_12901").toString()) : 0) +
(issue.get("customfield_12902") != null ? float.parseInt(issue.get("customfield_12902").toString()) : 0) +
(issue.get("customfield_12903") != null ? float.parseInt(issue.get("customfield_12903").toString()) : 0) +
(issue.get("customfield_12904") != null ? float.parseInt(issue.get("customfield_12904").toString()) : 0)
) / 5;
return val;
-->
<!-- @@Format: 
numberTool.format("#0.00",value);
-->

I hope this helps point you in the right direction even if the exact format of the commands is slightly out.

 

Phill

Thanks Phil; It's been quite some time since I've written a calculated field with that add-on. :)

1 vote
Ignacio Pulgar Community Champion Dec 02, 2015

It would have been enough to divide by 5.0 instead of 5.

int / int = int

int / float = float

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Posted 13 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

36 views 0 5
Join discussion

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