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

Format numbers as currency

Grika
Registered: Dec 9 2009
Posts: 35
Answered

I am pulling values from fields formatted for number -> currency but the values that populate the variables are straight numbers. e.g. the field contains 12345 and when it loses focus, it automatically converts it to 1,2345.00.

I need the variable to also convert the number to the latter format.

My Product Information:
Acrobat Pro 8.1.7
try67
Expert
Registered: Oct 30 2008
Posts: 2398
What do you mean, exactly?
If the field has formatting applied to it, the values you apply in a script will have that formatting.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Grika
Registered: Dec 9 2009
Posts: 35
I have the formatting set by the field properties to display numbers in currency format ($1,234.00). I have a button that assigns a variable the data from said field, but the resulting value in the variable is 1234.

I need the number in the variable to also be in "comma decimal" format or be able to format it when I use it somewhere else.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Did you try accessing valueAsString instead of value? I'm not sure that would work, though.
If it doesn't work, then you will have to apply the formatting in the code itself.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Grika
Registered: Dec 9 2009
Posts: 35
'Fraid that didn't work either.

Until I figure out something more clever this worked:
function formatCurrency(num) {num = num.toString().replace(/\$|\,/g,'');if(isNaN(num))num = "0";sign = (num == (num = Math.abs(num)));num = Math.floor(num*100+0.50000000001);cents = num%100;num = Math.floor(num/100).toString();if(cents<10)cents = "0" + cents;for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));return (((sign)?'':'-') + '$' + num + '.' + cents);}

And then using this:
   // Get the DME and convert for currencyvar cDME = formatCurrency(this.getField("DME Max").value);
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Personally, I would let the field perform the formatting and if I needed to use value in a message I would the "util.printf()' method for formatting the display format or performing the rounding. If you need to round the value for a field value, the 'util.printf()' method can also perform that task.

Acrobat JavaScirpt does not provide the formatted string for the 'value' property to allow for numerical computation. But the value is not the rounded display value. So as the programmer you need to decide if you want the field to have the rounded value of the non-rounded computed value.

George Kaiser

Grika
Registered: Dec 9 2009
Posts: 35
Very interesting.

I tried:
   var cCont = util.printf("$%.2f", this.getField("Billing Rate").value);
The field contains:
1234567

The committed field (when it loses focus) is:
$1,234,567.00

The variable contains:
$1234567.00

How can I add commas as well?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You need to add the 'nDecSep' value for the format string.

// assign values to the parts of the format stringvar cCurrency = '$'; // currency symbolvar nDecSep = ',0'; // use ',' thousand separator and '.' decimal pointvar nFlags = ''; // no sign or leading spacevar nWidth = ''; // no maditory widthvar nPrecision = .2; // number of decimal placesvar cConvChar = 'f'; // floating number// build format stringvar cFormat = cCurrency + '%' + nDecSep + nFlags + nWidth + nPrecision + cConvChar;var cCont = util.printf(cFormat, this.getField("Billing Rate").value); // or the foramt string as a string within the method:var cCont = util.printf('$%,0.2f', this.getField("Billing Rate").value);

George Kaiser

Grika
Registered: Dec 9 2009
Posts: 35
Brilliant! That tidies my code nicely.


Thank you very much.