Issue
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."
Solution
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');
}
Answered By - Srijon Chakraborty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.