HOWTO · jQuery
How to Convert String to Number in jQuery
This tutorial demonstrates converting a string to a number using jQuery.
On this page
When a value comes from a jQuery input, .val() returns a string. Convert that string with JavaScript before doing arithmetic. Use Number() when the whole value must be numeric; use parseInt() or parseFloat() when accepting a numeric prefix is intentional. These are JavaScript functions and operators used alongside jQuery, not jQuery conversion methods.
Convert a String to a Number in jQuery
Choose the conversion based on what the input is allowed to contain:
Number(value)checks the whole trimmed string and is usually the clearest choice for form values.parseInt(value, 10)reads an integer prefix;parseFloat(value)reads a decimal prefix.- Unary
+valueandvalue * 1coerce a value likeNumber(). Math.floor(value)andMath.ceil(value)coerce and then round; they do not parse a numeric prefix.
For example, read a value with const raw = $("#amount").val();, convert it, and display it with $("#result").text(number);. Check the result before displaying or calculating with it, because failed strict conversion produces NaN. The executable example below uses ordinary JavaScript so it can run without loading jQuery; the equivalent jQuery operations are .val() and .text() as shown above. Its observed output is 42.5.
Run this example:
const raw = "42.5";
const amount = Number(raw);
console.log(amount);
42.5
Use Number() for Strict Conversion
Number() converts a complete numeric string to a JavaScript number. It accepts decimal notation, surrounding whitespace, and an empty or whitespace-only string (which becomes 0). A comma-formatted value such as "99,000" is not valid numeric notation for Number(), so it becomes NaN. Text before or after digits, such as "1000 items", also produces NaN; this strict behavior helps detect unexpected form input rather than silently accepting part of it.
Use Number() when the field should contain only a number. To validate the conversion, test Number.isFinite(number): it returns false for NaN and positive or negative Infinity, and true for finite numbers. If a blank field should count as missing rather than as zero, check raw.trim() === "" before converting.
Use parseInt() and parseFloat() for Prefix Parsing
parseInt() reads an integer prefix, while parseFloat() reads a decimal prefix. Both ignore leading whitespace and stop at the first character that cannot continue the number. Thus, parseInt("42px", 10) returns 42, and parseFloat("3.75kg") returns 3.75. The radix argument 10 makes the intended base explicit for ordinary decimal integers.
This prefix behavior also means parseFloat("99,000") returns 99, not ninety-nine thousand: parsing stops at the comma. A string that does not begin with a valid numeric prefix, such as "items 42", returns NaN. parseInt("13.9", 10) returns 13 because integer parsing stops at the decimal point; it does not round. Pick prefix parsing only when trailing units or other suffixes are expected and acceptable. Otherwise prefer Number() and validate the entire field.
Use Unary + or * 1 for Numeric Coercion
Unary plus (+raw) and multiplication by one (raw * 1) ask JavaScript to coerce the value to a number. For strings, they behave like Number() on the inputs discussed here: decimal strings convert, while comma-formatted strings and strings containing other text produce NaN. Empty and whitespace-only strings coerce to 0 as well. Unary plus is concise; Number() is often more readable when conversion intent matters. There is no need to choose between them based on an unverified speed claim.
Use Math.floor() and Math.ceil() to Round a Number
Use Math.floor() or Math.ceil() after conversion when the task requires rounding. Both first coerce their argument to a number, so invalid numeric text still yields NaN. Math.floor() returns the greatest integer less than or equal to its input, while Math.ceil() returns the least integer greater than or equal to it. For a positive decimal such as 13.21, floor gives 13 and ceil gives 14. For negative values, floor moves toward negative infinity, not toward zero; for example, flooring -13.21 gives -14. If the decimal should remain intact, do not round it.