2

How to check if a string is a float/int and not a NaN in Angular8?

I couldn't find any answers relevant to Angular8

1

2 Answers 2

4

TypeScript is a superset of JavaScript. So we can use JavaScript code in TypeScript with some slight modifications:

isIntOrFloat(n){
    return !isNaN(n);
}

Vanilla JavaScript version:

function isIntOrFloat(n){
    return !isNaN(n);
}        
    
console.log(`is int 1? ${isIntOrFloat("1")}`)
console.log(`is int 1.5? ${isIntOrFloat("1.5")}`)
console.log(`is int a? ${isIntOrFloat('a')}`)
console.log(`is int b? ${isIntOrFloat('b')}`)

Sign up to request clarification or add additional context in comments.

Comments

2

As StepUp mentioned, typescript is a superset of javascript, you can use all javascript functionalities inside typescript.

const notNumber= (str) => isNaN(str)

notNumber(12)// returns false

notNumber("sd")// returns false

Comments

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.