How can I parse this string "451:45" to a number? What I want to get is "451.45" as number.
Thanks in advance
CodePudding user response:
Replace the colon [:] with the dot [.] by using
replace
method of JS as -'451.45'.replace(":", ".");
Now, we have desired string but as we need number so just parse it into Float by using
parseFloat
as -parseFloat('451.45');
Please find the complete code as -
let str = '451:45'; str = str.replace(":", "."); str = parseFloat(str);
Let me know in comments if you need any help further.
Thanks.
CodePudding user response:
so:
console.log( "451:45".replace(':', '.'));
i.E:
private parseMyNumber(n: string): number{
return n.replace(':', '.');
}
CodePudding user response:
If you're using angular and typescrip I use this code
const stringNumber = '451:45';
console.log(Number(str.replace(":", ".")));
I don't know if you have it inside a function in that case would be something like this
stringReplaceToNumber(string: string) {
return Number(string.replace(":", "."))
}