-1
var number1 = Number("1123 mm");

I want to parse this String to a number, 1123 to be exactly. However, I get NaN if I do this. Does anyone have a suggestion how I could parse this to a number?

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
mambo150
  • 13
  • 1
  • 2
  • duplicate http://stackoverflow.com/questions/14667713/typescript-converting-a-string-to-a-number#14668510 – danywalls Mar 10 '17 at 14:50
  • Possible duplicate of [TypeScript Converting a String to a number](http://stackoverflow.com/questions/14667713/typescript-converting-a-string-to-a-number) – Daniel A. White Mar 10 '17 at 14:52

2 Answers2

6

Just as in JavaScript, you can use parseInt and parseFloat.

var number1 = parseInt("1123 mm", 10); // 1123
var number2 = parseFloat("115.65 lkjsdf"); // 115.65
Fenton
  • 206,497
  • 63
  • 356
  • 369
1

Number only works for valid number strings and booleans.

You want to use parseInt()

var number1 = parseInt("1123 mm");
fmacdee
  • 2,153
  • 6
  • 14