Kode_12 - 3 months ago 25

Javascript Question

I have an input field that accepts numeric values. I'd like to find a method that converts that that string value into a number value.

**ParseInt()** was what first came to find, then **toFixed()**.

What do these have in common? It rounds the values to its Integer value (like ParseInt suggests--I know!).

How can I convert a string representation into a number value while retaining any decimal values?

ex:

`"54.3" --> 54.3`

I apologize if this is a duplicate question.

Answer

An integer is a whole number.

A Float is a number with decimal values.

Knowing this you will want to use `parseFloat()`

, not which will take the period into consideration, instead of `parseInt()`

which will round to the nearest whole number

Refer to: these docs for more detailed information

`parseFloat("string");`

`let a = "54.3"`

`a = +a;`

Doing +a is practically the same as doing a * 1; it converts the value in `a`

to a number if needed, but after that it doesn't change the value.