Illep
Illep

Reputation: 16841

Angular typescript convert string to number

I want to convert a string number (an integer represented as a string. ex "12" ) to a number

Model Class

export class Person{
  FirstName :  string | undefined ;
  Telephone:  number | undefined ;

}

TS file

  console.log("Person" + JSON.stringify(this.person));

The JSON appears as

{
    "FirstName ": "hhhhh",
    "Telephone": "1478525",
}

The Telephone property is a number and I want it to appear as follows (without the ""_ :

{
    "FirstName ": "hhhhh",
    "Telephone": 1478525,
}

Approaches I took.

this.person.Telephone = parseInt(telephone); 

Above didn't work, it shows the same number enclosed as a string. I also tried the following:

this.person.Telephone as number

both approaches didn't work. Can someone help me solve this ?

Error I get if I don't convert to a number:

"The JSON value could not be converted to System.Int32. Path: $.Telephone | LineNumber: 2 | BytePositionInLine: 18."

Upvotes: 4

Views: 24253

Answers (6)

Steph TwX
Steph TwX

Reputation: 87

  ngOnInit(): void {
    const stringValue: string = '341';
    const numberValue: number = +stringValue;
    // numberValue === 341
  }

Upvotes: 0

kumarras
kumarras

Reputation: 136

One scenario where the string to number conversion might be needed is while fetching the params from the activated route in Angular. The easiest way is to append a + operator at the beginning.

constructor(private serversService: ServersService, private actRoute: ActivatedRoute) { }



ngOnInit() {
    const id = +this.actRoute.snapshot.params['id'];//convert param string to number if needed
    this.server = this.serversService.getServer(id);
    this.actRoute.params.subscribe((params: Params) => {
      this.server = this.serversService.getServer(+params['id']);
    })
  }

Upvotes: 0

Srikar Phani Kumar M
Srikar Phani Kumar M

Reputation: 1374

If you want to get a number from a string, you can use this approach.

let person = {
name: "Some Name",
age: "45"
};

console.log('Person obj before modification ==>', person);

person.age = parseInt(person.age, 10);

console.log('Person object after modifying ==>', person);

Upvotes: 1

Akif Hadziabdic
Akif Hadziabdic

Reputation: 2890

You can use unary operator +

export class Person{
  FirstName :  string | undefined;
  Telephone:  number | undefined;
}
let person = new Person();
person.FirstName = "Name";
person.Telephone = 12345;
console.log(person);
person.Telephone = +"12345";
console.log(person);

Output:

Person { FirstName: 'Name', Telephone: 12345 }
Person { FirstName: 'Name', Telephone: 12345 }

PS: Use string as the data type for phone numbers.

Upvotes: 1

zerocewl
zerocewl

Reputation: 12784

Just use the standard javascript Number

this.person.Telephone = Number(telephone); 
console.log("Person" + JSON.stringify(this.person));

Upvotes: 2

Srijon Chakraborty
Srijon Chakraborty

Reputation: 2154

I think you can try this =>

let stringToNumberData = "123";
let numberValue = Number(stringToNumberData);
console.log(numberValue);
//Returns 123

OR

if(!isNaN(Number(stringToNumberData ))){
  let numberValue = Number(stringToNumberData );
  console.log(numberValue);
} else{
    console.log('Not a Number');
}

Upvotes: 4

Related Questions