4 Replies Latest reply on Sep 19, 2008 12:35 PM by (Bret_Orsburn)

# String to Number conversion

Hey all again,

Is there a method or something to take a string like "21,000.00" and convert it to a number looking like "20000.00" so that I can run a calculation on it?

util,printf doesn't seem to work.

parseInt isn't part of acrobat to my knowledge

parseFloat isn't part of acrobat to my knowledge

Help!

Bret
• ###### 1. Re: String to Number conversion
parseInt does indeed work in Acrobat, since it is part of JavaScript 1.5.
• ###### 2. Re: String to Number conversion
In the case you posted, parseInt won't give you what you want:
```var s = "21,000.00";
var n = parseInt(s); // result = 21, not 21000

```

If you were to first remove the comma, it would work:

```var s1 = "21,000.00";
var s2 = s1.replace(",", "");
var n = parseInt(s2); // result = 21000

```

George
• ###### 3. Re: String to Number conversion
As with most JavaScript, there are multiple ways to test and to accomplish an end.

It appears the offending character that is preventing the conversion of a string to a number is the inclusion of the thousand separator of ",". Removing this character allows any number of methods to work.

One can use the "typeof " method to find the type of an item, like string or number for a variable; or "isNaN()" method to test for "Not a Number" of a parameter, true is not a number and false is a number.

There is also the "Number()" constrictor to convert a string to a number or zero.

The following script run on the cosnole:

// paresInt
console.println("parseInt(): " ) ;
var s1 = "21,000.00";
console.println("s1: " + s1 + " typeof: " + typeof s1 + " - isNaN(s1): " + isNaN(s1) );
var s2 = s1.replace(",", "");
console.println("s2: " + s2 + " typeof: " + typeof s2 + " - isNaN(s2): " + isNaN(s2) );
var n = parseInt(s2); // result = 21000
console.println("n: " + n + " typeof: " + typeof n + " - isNaN(n): " + isNaN(n) );
//
// parseFloat
console.println(' ');
console.println("parseFloat(): " ) ;
s1 = "21,000.00";
console.println("s1: " + s1 + " typeof: " + typeof s1+ " - isNaN(s1): " + isNaN(s1) );
s2 = s1.replace(",", "");
console.println("s2: " + s2 + " typeof: " + typeof s2 + " - isNaN(s2): " + isNaN(s2) );
n = parseFloat(s2); // result = 21000
console.println("n: " + n + " typeof: " + typeof n + " - isNaN(n): " + isNaN(n) );
//
// Number
console.println(' ');
console.println("Number(): " ) ;
s1 = "21,000.00";
console.println("s1: " + s1 + " typeof: " + typeof s1 + " - isNaN(s1): " + isNaN(s1) );
var s2 = Number(s1.replace(",", ""));
console.println("s2: " + s2 + " typeof: " + typeof s2 + " - isNaN(s2): " + isNaN(s2));
//
// Number of a character
console.println(' ');
console.println("Number(\"A\" ): " );
s1 = "A";
console.println("s1: " + s1 + " typeof: " + typeof s1 + " - isNaN(s1): " + isNaN(s1) );
var s2 = Number(s1.replace(",", ""));
console.println("s2: " + s2 + " typeof: " + typeof s2 + " - isNaN(s2): " + isNaN(s2));

Produces the following results:

parseFloat():
s1: 21,000.00 typeof: string - isNaN(s1): true
s2: 21000.00 typeof: string - isNaN(s2): false
n: 21000 typeof: number - isNaN(n): false

Number():
s1: 21,000.00 typeof: string - isNaN(s1): true
s2: 21000 typeof: number - isNaN(s2): false

Number("A"):
s1: A typeof: string - isNaN(s1): true
s2: NaN typeof: number - isNaN(s2): true

undefined
• ###### 4. Re: String to Number conversion
Thanks all! With the below code, if you enter 11,111.11, you will get a NUMBER value of 11111.11 which is perfect for what I need. You can calculate with it and such.

var variable1 = Number((field1.valueAsString).replace(",", ""));