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

@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