Forums

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

How to auto format number fields using scriptrunner

Scott Federman February 8, 2022

Is it possible to auto-format a number field in scriptrunner?

Ex:

Cap: 12.335 should be 12.34

Cap: 12.3 should be 12.30

Cap: 12 should be 12.00

 

1 answer

0 votes
Pramodh M
Community Champion
February 8, 2022

Hi @Scott Federman 

Can you try this and let me know if this works?

def cap = 12.335
cap = ((float)cap).round(2)
println cap

Thanks,
Pramodh

Pramodh M
Community Champion
February 8, 2022

If you really need that 0 after a decimal

Convert the number to string and append zero

Scott Federman February 8, 2022

how would i do that? would i need a text field vs number field?

Pramodh M
Community Champion
February 8, 2022

Here it is @Scott Federman 

Let me know if this works

def cap = 2.34
cap = ((float)cap).round(2)

cap = ((String)cap)
str = cap.split("\\.");
if (str[1].length() == 1)
cap = ((String)cap) + "0"

println cap

Thanks

Scott Federman February 9, 2022

@Pramodh M it returns this error "The scripted field ran successfully, but returned an invalid type. Please return a number" There is only a number in the field

Pramodh M
Community Champion
February 9, 2022

Hi @Scott Federman 

If you are returning a number, 2 zero after the decimal is not possible

You need to use this

def cap = 2.34
cap = ((float)cap).round(2)
Scott Federman February 9, 2022

Yeah no joy there either. This is for cloud?

Pramodh M
Community Champion
February 9, 2022

@Scott Federman 

May I know what is the error?

Scott Federman February 10, 2022 edited

@Pramodh M i converted this to a text field called Cap-test. Changed the script to 

def cap-test = 2.34
cap-test = ((float)cap-test).round(2)

cap-test = ((String)cap)
str = cap-test.split("\\.");
if (str[1].length() == 1)
cap-test = ((String)cap-test) + "0"

println cap-test

and returned this error 

Capture.JPG

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, mindful member, mindful member badge, atlassian community, community kudos, community giveaway, atlassian swag, community values, empathy and kindness, badge challenge, atlassian learning, community engagement, swag giveaway

Earn a Badge, Win a Prize 🎁

Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!

Start here
AUG Leaders

Atlassian Community Events