# AnsweredConverting String to Number in JavaScript: Need Clarification and Code Example

#### Hilton D

##### Coder
I'm trying to convert a string to a number in JavaScript, but I'm encountering some unexpected results. While I've used functions like parseInt() and parseFloat(), I'm still confused about the behavior in certain cases. Could someone clarify the process of converting a string to a number and provide a code example?

Here's what I've tried so far:

Code:
``````let stringNumber = "123";
let parsedInt = parseInt(stringNumber);
let parsedFloat = parseFloat(stringNumber);

console.log(typeof parsedInt, parsedInt); // Output: number 123
console.log(typeof parsedFloat, parsedFloat); // Output: number 123``````

However, when I encounter strings with non-numeric characters or decimal points, the results seem inconsistent:

JavaScript:
``````let nonNumericString = "abc";
let stringWithDecimal = "45.67";

let parsedNonNumeric = parseInt(nonNumericString);
let parsedWithDecimal = parseInt(stringWithDecimal);

console.log(parsedNonNumeric); // Output: NaN
console.log(parsedWithDecimal); // Output: 45``````

Can someone explain why the conversion behaves this way and how I can reliably convert strings to numbers, even when dealing with non-numeric characters or decimals? I'd appreciate a clear explanation and some code examples to illustrate the correct practices. Thank you in advance for your help!

Solution
An integer is a whole number so when using `parseInt()` on a decimal number, it's just returns the whole number and ignores the decimal value.
To convert a string with decimal number to a number, you will have to use the `parseFloat()` function.

Generally you will want to use `parseFloat()` as it supports decimal numbers and exponentials (for example `parseFloat("3e2")` will return 300 rather than 3 being return when using `parseInt()`), unless you specifically want to convert a floating-point number into an integer, rounding down.

how I can reliably convert strings to numbers, even when dealing with non-numeric characters
It's not possible to convert non-numerical characters to a number.
The function...
An integer is a whole number so when using `parseInt()` on a decimal number, it's just returns the whole number and ignores the decimal value.
To convert a string with decimal number to a number, you will have to use the `parseFloat()` function.

Generally you will want to use `parseFloat()` as it supports decimal numbers and exponentials (for example `parseFloat("3e2")` will return 300 rather than 3 being return when using `parseInt()`), unless you specifically want to convert a floating-point number into an integer, rounding down.

how I can reliably convert strings to numbers, even when dealing with non-numeric characters
It's not possible to convert non-numerical characters to a number.
The function will return NaN, which means Not a Number.

I have always used Number(). To convert a strig to a number and have never had any problrms.

Yeah, parseInt means make an integer (without a decimal) of a string and parseFloat makes a decimal number of a string. Another method is new Number(string) and yet another I have tried is string-"" but please prefer previous three and not -"" as that can produce errors. eval(string) may also be used to translate equations but may produce some other than number results. NaN is not a number and is still like a number in that it is Number class type thing.
X E.