4

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."

5
  • 3
    Yes. Dont convert telephone to a number. A telephone number is not a number, it should be represented by a string. A number is something numeric, if you are wanting to know is it a number or a string ask yourself if it would make sense to perform a numeric operation on it. Would you ever add a number to a telephone number or subtract from it or divide by it. What about numbers in countries that start with 0? Those would have a problem if you convert it to a number. In the Netherlands almost all mobile numbers start with 06. Commented Feb 4, 2021 at 19:48
  • I get an error if I don't convert. Please look at updated post for the error. I tried adding a number (as integer ) from postman when sending request to server and it works then. So I need to make it a pure Integer. Commented Feb 4, 2021 at 19:50
  • Ok @Igor Let's say it's not Telephone but, age. Because I have another property which also has the same issue. Commented Feb 4, 2021 at 19:51
  • In that case parseInt is the way to go when you want to convert a string to a number. If you want more help you need to create an minimal reproducible example because "Above didn't work, " is not descriptive enough as this.person.Telephone = parseInt(telephone); should work just fine. Commented Feb 4, 2021 at 19:53
  • just change your class definition for the telephone property to be a string not a number export class Person{ FirstName : string | undefined ; Telephone: string| undefined ; } Commented Feb 4, 2021 at 19:57

6 Answers 6

4

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');
}
Sign up to request clarification or add additional context in comments.

Comments

2

Just use the standard javascript Number

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

Comments

1

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.

Comments

1

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

Comments

0

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']);
    })
  }

Comments

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

1 Comment

Please keep in mind that +"" evaluates to 0 whereas parseInt("") returns NaN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.