Skip to main content
m.raby
Participating Frequently
March 1, 2019
Answered

How to convert string to number if string contains $

  • March 1, 2019
  • 1 reply
  • 3895 views

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

This topic has been closed for replies.
Correct answer Thom Parker

You must also remove the commas.

Replace line #2 in the code above with this:

strPrice1 = strPrice1.replace(/,/g, "").replace(/^\$/, "");


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);

1 reply

JR Boulay
Community Expert
March 1, 2019

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);

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
March 1, 2019

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);

Thom Parker
Community Expert
March 8, 2019

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.


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?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often