Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Data formatting question - percentages

csbendz
Level 2
Level 2
I am trying to get an output in a percentage form, eg 89% when a user types in 89 and/or 0.89



I've been trying to format it on my own for some time with no luck- can anyone help?
3 Replies
Steve_L_Walker
Level 10
Level 10
Carly,



There are a number of options. If you define the field as a Decimal Field or a Numeric there are many pattern options for Display and Edit (on the object Field tab, click Patterns, and go to the Display or Edit tab).



If you want to use a Text Field then life becomes a little more complicated. The following script could be put on the exit event. If the field is not null it will check that the value entered is a number. If it is a number it will convert the string accordingly.



if (!(this.formattedValue.length == 0 && this.editValue.length == 0)) {

if (isNaN(this.rawValue)) {

xfa.host.messageBox("This is not a number.");

}

else {

var myNum = parseFloat(this.rawValue);

if (myNum < 1) {

myNum = myNum * 100;

}

this.rawValue = myNum.toString() + "%";

}

}



Please test this thoroughly. It is late...



Steve
csbendz
Level 2
Level 2
This is helpful, thank you.



My follow up question is: how can I enable the user to enter a whole number, and have the percentage sign just automatically appear? (As of right now, the user has to enter a decimal in order for the percent to display. I don't want the user to have to enter a decimal - it's not the end of the world, but it isn't ideal.) I have been using numerical fields.
Steve_L_Walker
Level 10
Level 10
Carly,



Add the display pattern



num{(zz9'%')}



This enables a value of 1-999 to be entered and displayed with a trailing percent sign. For example, enter 5 and the result is 5%, enter 100 and the result is 100%.



Steve