Skip to content

JavaScript string to decimal | Convert Example code

  • by

Use any one method parseInt() or parseFloat() or Number() to convert string to decimal in JavaScript. If want a whole number then the parseInt() method.

Use parseFloat() to convert string to float number and can use toFixed() to fix decimal points

JavaScript string to decimal

Simple example code.


Where the first argument is the string to convert and the second argument (optional) is called the radix. This is the base number used in mathematical systems.

<!DOCTYPE html>


    var text = '42px';
    var integer = parseInt(text); // parseInt(text, 10)




JavaScript string to decimal


This method converts a string into a decimal point number. You can even pass in strings with random text in them.

var text = '3.14someRandomStuff';
var pointNum = parseFloat(text);
// returns 3.14


No need for a radix and usable for both integers and floats

Number('123'); // returns 123
Number('12.3'); // returns 12.3
Number('3.14someRandomStuff'); // returns NaN
Number('42px'); // returns NaN

The parseFloat() function parses a string argument and returns a floating-point number. It will try to extract and convert the numeric value from the beginning of the string until it encounters an invalid character. If the string cannot be parsed into a valid number, it will return NaN (Not-a-Number).

Note: parseFloat() will ignore leading and trailing white spaces in the string but will stop parsing as soon as it encounters a non-numeric character, including whitespace between or after the digits.

Do comment if you have any doubts or suggestions on this JS string topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *