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

m.raby
m.rabyAuthor
Participating Frequently
March 1, 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.