Highlighted

Data formatting question - percentages

csbendz

17-02-2009

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?

Replies

Highlighted

Steve_L_Walker

20-02-2009

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
Highlighted

csbendz

20-02-2009

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.
Highlighted

Steve_L_Walker

20-02-2009

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