These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Changing number of characters behind decimal

shawnash
Registered: Dec 16 2009
Posts: 48

Hello,

I have a field called Rate that needs to have 2 decimal places if another field called Grade starts with a G, and if it doesn't start with a G then Rate needs to be 4 decimal places. I am almost sure that it needs code inserted in the custom format script section, but am not sure how to code it. I am using Acrobat 5. Does anyone have any suggestions on what I need to do to accomplish this?

Thanks in advance,
Shawn

My Product Information:
Acrobat Standard 5.x or older, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
If the field is read only and has an associated calculation script, you can make the field a text field and then use the 'util.printf()' method to create the formatted display results.

Another option would be to use the 'setAction' method to set the format

var cGrade = this.getField('Grade').value.substr(0,1); //get 1st character of grade fieldvar f = this.getField('Rate'); // get field objectif (cGrade == 'G') {f.setAction("Format", ’AFNumber_Format(2, 0, 0, 0, "", true));} else {f.setAction("Format", ’AFNumber_Format(4, 0, 0, 0, "", true));}
Quote:
AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)nDec = number of decimals
sepStyle = separator style 0 = 1,234.56 / 1 = 1234.56 / 2 = 1.234,56 / 3 = 1234,56 /
negStyle = 0 = black minus / 1 = red minus / 2 = parens black / 3 = parens red /
currStyle = reserved
strCurrency = string of currency to display
bCurrencyPrepend true = pre pend / false = post pend

George Kaiser

shawnash
Registered: Dec 16 2009
Posts: 48
Hello,

I tried the code below because the field is not read only, but it gave me this error:
TypeError: this.getField("Grade").value.substr is not a function
What am I doing wrong?

var cGrade = this.getField('Grade').value.substr(0,1); //get 1st character of grade field
var f = this.getField('Rate'); // get field object
if (cGrade == 'G')
{
f.setAction("Format", 'AFNumber_Format(2, 0, 0, 0, "", true)');
}
else
{
f.setAction("Format", 'AFNumber_Format(4, 0, 0, 0, "", true)');
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I would avoid using the "setAction" function in a document script since it will not work in Reader. The problem whith "substr" function is that it works on strings, and the value returned from the field could be null, a number, or a string. Use this:

this.getField('Grade').valueAsString.substr(0,1);

Or better yet, use this format script in the Rate field. It will work in all situations and all versions of Acrobat/Reader
if(/^G/.test(this.getField("Grade").valueAsString))event.value = util.printf("%0.2f",event.value);elseevent.value = util.printf("%0.4f",event.value);

Using a regular expression for the test and "util.printf" for the formatting make short work of a conditional format script

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script