Copy link to clipboard
Copied
I have a form importing data from a .fdf and has fields with $ in it. For example "Selling_Price" value pulls from .fdf file "$51,480.00" but this is not a number it is just a text string.
How do I convert this into a number of 51,480.00 for another field so I would be able to use 51,480.00 to add to another field with an actual number value?
Example
event.value = this.getField("Selling_Price").value + this.getField("Sales_Tax").value;
I have tried
//Convert String to Number
event.value = Number(getField("Selling_Price").value);
and I have tried
//Convert String to Number
event.value = Number(this.getField("Selling_Price").value);
How can I achieve this when the text field contains "$" in the field?
I am importing
Copy link to clipboard
Copied
I think you could get away with a simpler RegExp. Just remove the offending characers and don't worry about format.
strPrice1 = strPrice1.replace(/[$,]/g, "");
if(!isNaN(strPrice1))
strPrice1 = Number(strPrice1);
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hi.
You should try something like this:
var strPrice1 = this.getField("Selling_Price").valueAsString;
var strPrice2 = strPrice1.shift(); // remove the first car. of the string
var strSales1 = this.getField("Selling_Price").valueAsString;
var strSales2 = strSales1.shift(); // remove the first car. of the string
event.target.value = Number(strPrice2) + Number(strSales2);
Copy link to clipboard
Copied
I think you got some of the field and variable names mixed up... Also, That's a bit dangerous because you don't know for sure that that first character is a dollar symbol and if at some point the input will change it will be something else you might cause it to display an incorrect calculation.
I would add some kind of condition to it, like:
var strPrice1 = this.getField("Selling_Price").valueAsString;
if (/^\D/.test(strPrice1)) strPrice1 = strPrice1.shift(); // remove the first character of the string IF it's non numeric
var strSales1 = this.getField("Sales_Tax").valueAsString;
event.target.value = Number(strPrice1) + Number(strSales1);
Copy link to clipboard
Copied
Neither method seems to work. I have tried them every way possible I could think of. I even created new fields just to strip it down to the basics of just trying to get a number without the $ symbol.
All methods I could think of either return nothing or Nan
When I stripped it down to just
var a = this.getField("SELLING_PRICE").valueAsString;
event.value = a
I do get the string value of $28,353.86 but I still can't get this to a number.
Copy link to clipboard
Copied
You must also remove the commas.
Replace line #2 in the code above with this:
strPrice1 = strPrice1.replace(/,/g, "").replace(/^\$/, "");
Copy link to clipboard
Copied
I think you could get away with a simpler RegExp. Just remove the offending characers and don't worry about format.
strPrice1 = strPrice1.replace(/[$,]/g, "");
if(!isNaN(strPrice1))
strPrice1 = Number(strPrice1);
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
The dollar symbol must be escaped.
Copy link to clipboard
Copied
You know, I thought the same thing, but apparently when it is used in the "one of" selection brackets its interpreted as a literal. This makes sense, since it is a location identifier instead of a character identifier.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
You're right, it does work when placed in square brackets... You learn something new every day.
Copy link to clipboard
Copied
This formula along with the one you wrote earlier seems to work the best. The one with the [] that Thom Parker​ wrote still seems to somewhere be broken. Now it could be in the way I am writing the formula.
How do you get replace other common symbols like %@ () etc? Thanks everyone for the help on this. I have been able to get it functioning 99% correctly. Not sure why importing sometimes still has some fields as NaN until I basically type in one field and change it then change it right back for the calculations to work properly. But at least I am able to start using everything I have been working on.
Copy link to clipboard
Copied
Thom's method should work and it's easier to expand... You can include all symbols you want to remove inside the square brackets, although some symbols might have to be escaped, if they are reserved for other functions in the Regular Expression syntax.
To "escape" something basically means to put a back-slash before it, by the way.
What you're describing is usually the result of an incorrect field calculation order. It has nothing to do with the code.
Copy link to clipboard
Copied
You can find out more about matching text with regular expressions here:
https://acrobatusers.com/tutorials/text-matching-regular-expressions
Could you post the values for which the code I provided is not working?
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Neither method seems to work
Mine, perhaps.
But Try67's method must work, he is a JavaScript Jedi!

