Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to convert string to number if string contains $

Community Beginner ,
Feb 28, 2019 Feb 28, 2019

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

TOPICS
PDF forms
3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 01, 2019 Mar 01, 2019

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

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 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);

PDF Acrobatic, InDesigner & Photoshoptographer
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 01, 2019 Mar 01, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 2019

You must also remove the commas.

Replace line #2 in the code above with this:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 2019

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 2019

The dollar symbol must be escaped.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 2019

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.

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 2019

You're right, it does work when placed in square brackets... You learn something new every day.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 08, 2019 Mar 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2019 Mar 08, 2019

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2019 Mar 08, 2019
LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2019 Mar 01, 2019

Neither method seems to work

Mine, perhaps.

But Try67's method must work, he is a JavaScript Jedi!

PDF Acrobatic, InDesigner & Photoshoptographer
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines