Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Math expressions resulting in two digits

ray September 22, 2023

Is it possible with math expressions to have a result of always 2 digits?

f.e. 
{{#=}}1+2{{/}} = 03 instead of just 3
{{#=}}1+10{{/}} = 11

I was trying to do a check with IF(condition,value_if_true,value_if_false) to place a "0" in front of the result when it's <10 but that didn't work out...

 

2 answers

0 votes
ray September 23, 2023

Hi Bill;

The outcome of the math should be in the summary-field, so a text field. I can f.e. fetch the week number by adding xxxxxxx.format("ww"). But whenever i'm calculating someting like f.e. {{#=}}1+2{{/}}, then the result is always 3 and not 03 :)

0 votes
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 22, 2023

Hi @ray 

Are you asking about text fields (or output) or number fields?  I text fields, that is possible, but it is not possible to store a number field as the leading digits would drop when saved.

Let's assume your output is either a text field or output to the log, email, or a text field...

I believe if you use the inline math operations instead, you could add the format() function on the end to get that result.

{{someField.plus(someNumber).format("000")}}     -- this would prefix 3 digits with leading zeros

However if you are using numbers without any field references, you would do this in three steps:

  1. perform your math operation and save the value in a text field
  2. re-fetch the issue to reload the saved value
  3. update the field, using the leftPad() function to set your number of leading digits

Kind regards,
Bill

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 23, 2023

Ray, please try to stick to one thread when responding.  That will help others in the future know if there are multiple answers to a question.  Thanks!

Would you please explain what problem you are trying to solve?

I suggested a method to format numbers stored in fields/smart values as text with left-padding.  However if you have a known, literal expression like {{#=}}1+2{{/} why wouldn't you just replace that with "03" instead of doing the math?

ray September 24, 2023

Ok :) 

To respond to your question: because the summary is based on several calculations to number the task based on week numbers. But not only weeknumber, because weeknumber only could be formatted with xxxxx.format("ww"). But whenever I try to f.e. add +1 to the resulted weeknumber, the formatting will not by 2 digits, but only when when it's below 10.

This is my calculation:
{{triggerIssue.customfield_13403.format("ww")}}-FLOOR({{triggerIssue.customfield_13403.format("ww")}}/2){{/}}

the result should be f.e. 03 instead of 3 when it's below 10. I could hard code a "0" as prefix, but that's a manual solution 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 25, 2023

That is different than what you described, as you are in fact using issue fields.  Because of that you can write that as a single expression, using the inline, math operations.

{{triggerIssue.customfield_13403.format("ww").minus(triggerIssue.customfield_13403.format("ww").divide(2).floor).format("00")}}

ray September 26, 2023

Hi @Bill Sheboy 

thanks for the advice I tried your code but I couldn't get any result by using inline math. I also tried these to  find out the errors:

1. {{now.format("yyyy")}}
2. {{now.format("yyyy").divide(2)}}
3. {{now.format("yyyy").divide(2).format("0000")}}

only 1. gave me the result of 2023. The other 2 didn't give me any result

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 26, 2023

My mistake: I left off the conversion back to numbers after use of format() as that function leads to a text value.

Please try this:

{{triggerIssue.customfield_13403.format("ww").asNumber.minus(triggerIssue.customfield_13403.format("ww").asNumber.divide(2).floor).format("00")}}

ray September 27, 2023

Hi @Bill Sheboy 

Still no luck :(

I replaced the customfields with now, just to make sure that the error isn't in the customfields. I even tried to leave out the calculations to try to debug it.

 

Error rendering smart-values when executing this rule:
Failed to get value for now.format("ww").asNumber.minus(now.format("ww").asNumber:
1. {{now.format("ww").asNumber.minus(now.format("ww").asNumber}}

When only using {{now.format("ww").asNumber}},  the result is blank. Could it be a configuration error?
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 27, 2023

Would you please post images of your complete rule, the details of the action where you are using that expression, and of the audit log details showing the rule execution?  Those may provide some context for what is happening.  Thanks!

 

Until we see those...if you look at that error message it shows there is a missing right parenthesis for the minus() function call.

As a tip: the automation rule editor does not help with matching up curly-brackets, parentheses, etc.  It can help to write your expressions in a different text editor, and then to copy/paste them into your rule.

ray October 1, 2023

@Bill Sheboy 

There's not much in it actually since I created a test automation to test the expressions. The audit logs states succesfull but in reality the expressions are ignored resulting in blank input

Screenshot 2023-10-02 at 08.51.22.pngScreenshot 2023-10-02 at 08.52.29.png

Screenshot 2023-10-02 at 08.54.31.png

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 2, 2023

I am using Jira Cloud, I repeated your exact rule write to the comment and it worked for me...again.

Rules can get "glitched" with too many edits; I have no idea why this is, and...

We can check if that is the cause by trying this:

  1. Disable your rule, re-enable it, and test again.  If that does not help...
  2. Disable your rule, and re-created it as a new rule, and test again.  If that does not help...
  3. It appears there is something different with your install or with Jira Server. 

At that point, I'd recommend working with your site admin to contact Atlassian Support.  They may see something we are missing, or perhaps can explain if there is a difference for Server versus Cloud in the format() function.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events