Select to view content in your preferred language

how to change number format in python with code bloc

863
4
12-18-2023 06:39 AM
CeciliaMalvezzi
New Contributor

I would like to change my number format to percentage. I know I can make this in fields pane, but I wanna try to solve it in code bloc through "Calculate Field".

Image 1:

CeciliaMalvezzi_0-1702910296881.png

It's in this format that I wanna set, but in python in code bloc.

Many Thanks!!

 

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

Are you asking how to taking a number like 0.123 (12.3%) and make it 12.3?  If so, just multiply the current value by 100; otherwise, I am not sure what you are asking.  The numeric field will never store the percent sign, that is simply a formatting option in a table view.  If you want a percent sign with the actual data, you will have to use a string field.

CeciliaMalvezzi
New Contributor

I would like to transform the number "27" into "27%". The way that I know, is to going on field pane and changing "number format" to percentage.

 

Going in this string field idea, how do I concatenate the symbol "%" into my code bloc?

 

CeciliaMalvezzi_0-1702918159618.png

This scrip in code bloc helps me to get percentage value. If I wanna concatenate an "%" symbol, do I need to write "return round(X,0) + "%"". 

I'm asking cus when I made it, it acuses me error.

 

Many Thanks!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Python has very robust string formatting you can use and it won't require a code block.  For the expression, try:

"{:.0%}".format(!condicao!/!total!)
BlakeTerhune
MVP Regular Contributor

If you want to programmatically set the number format of a field, I don't think you can do that with Python.

0 Kudos