The Number object's parseInt() method
The Number.parseInt()
method parses a string argument and returns an integer of the specified radix or base.
Prerequisites
This tutorial assumes that you're familiar with:
SyntaxNumber.parseInt(string, radix)
Parametersstring:
The value to parse. If not a string, it's converted to one.
Leading whitespace is ignored
If the first character cannot be converted to a number, Number.parseInt() returns NaN
radix (optional):
Number.parseInt(string, radix)
string:
The value to parse. If not a string, it's converted to one.
Leading whitespace is ignored
If the first character cannot be converted to a number, Number.parseInt() returns NaN
An integer between 2 and 36 that represents the base of the number
Default is 10 (decimal) if not specified
Recommended to always specify to avoid unexpected results
Return value
The Number.parseInt()
method returns an integer parsed from the given string. If the string cannot be converted to an integer, it returns NaN
(Not a Number).
ExamplesExample 1: Basic integer parsing
In the code below, the function parseIntEx1()
demonstrates basic string-to-integer conversion.
function parseIntEx1() {
// Basic parsing
Logger.log(Number.parseInt("42")); // 42.0
Logger.log(Number.parseInt("42px")); // 42.0 (ignores non-numeric characters)
Logger.log(Number.parseInt("3.14")); // 3.0 (truncates decimal part)
// Leading whitespace is ignored
Logger.log(Number.parseInt(" 99 ")); // 99.0
// If first character isn't numeric, returns NaN
Logger.log(Number.parseInt("Apps123")); // NaN
// Negative numbers
Logger.log(Number.parseInt("-42")); // -42.0
}
Output:
42.0
42.0
3.0
99.0
NaN
-42.0
Example 2: Using different radices
In the code below, the function parseIntEx2()
shows how to parse integers with different number bases.
function parseIntEx2() {
// Binary (base 2)
Logger.log(Number.parseInt("1010", 2)); // 10.0 (decimal equivalent of binary 1010)
// Octal (base 8)
Logger.log(Number.parseInt("17", 8)); // 15.0 (decimal equivalent of octal 17)
// Decimal (base 10) - default
Logger.log(Number.parseInt("42", 10)); // 42.0
// Hexadecimal (base 16)
Logger.log(Number.parseInt("2A", 16)); // 42.0 (decimal equivalent of hex 2A)
Logger.log(Number.parseInt("0x2A")); // 42.0 (0x prefix recognized as hex)
// Different scenarios with hex
Logger.log(Number.parseInt("0xFF", 16)); // 255.0
Logger.log(Number.parseInt("#FF", 16)); // NaN (# is not valid for parseInt)
}
Output:
10.0
15.0
42.0
42.0
42.0
255.0
NaN
Example 3: Common pitfalls
In the code below, the function parseIntEx3()
highlights some best practices, common misconceptions and edge cases.
function parseIntEx3() {
// Always specify the radix to avoid confusion
Logger.log(Number.parseInt("08")); // 8.0 (modern JavaScript)
Logger.log(Number.parseInt("08", 10)); // 8.0 (explicitly decimal)
// Large numbers
Logger.log(Number.parseInt("1000000000000000")); // 1.0E15
Logger.log(Number.parseInt("1e6")); // 1.0 (only parses until invalid character)
// Empty or invalid inputs
Logger.log(Number.parseInt("")); // NaN
Logger.log(Number.parseInt(null)); // NaN
Logger.log(Number.parseInt(undefined)); // NaN
// Floating point conversion
Logger.log(Number.parseInt(3.99)); // 3.0 (truncates decimal)
Logger.log(Number.parseInt("3.99")); // 3.0 (truncates decimal)
}
Output:
8.0
8.0
1.0E15
1.0
NaN
NaN
NaN
3.0
3.0
Example 4: Comparing with global parseInt()
In the code below, the function parseIntEx4()
demonstrates how Number.parseInt()
relates to the global parseInt()
function. They're identical but using Number.parseInt()
makes it clear you're using a Number method
function parseIntEx4() {
// Number.parseInt() is identical to the global parseInt() function
Logger.log(Number.parseInt === parseInt); // true
// Both produce the same results
Logger.log(Number.parseInt("42", 10)); // 42.0
Logger.log(parseInt("42", 10)); // 42.0
// Both have the same behavior with edge cases
Logger.log(Number.parseInt("3.14")); // 3.0
Logger.log(parseInt("3.14")); // 3.0
}
Output:
true
42.0
42.0
3.0
3.0
Example 5: Practical use in Apps Script
In the code below, the function parseIntEx5()
demonstrates practical uses of Number.parseInt()
in Google Apps Script.
function parseIntEx5() {
// Extract numeric part from cell references
Logger.log(Number.parseInt("A42".replace(/\D/g, ""))); // 42.0
// Convert hex color code to RGB components
let hexColor = "#3366FF";
let r = Number.parseInt(hexColor.slice(1, 3), 16);
let g = Number.parseInt(hexColor.slice(3, 5), 16);
let b = Number.parseInt(hexColor.slice(5, 7), 16);
Logger.log("RGB: " + r + ", " + g + ", " + b);
// Parse user input from a form
let userInput = "42";
let numericValue = Number.parseInt(userInput, 10);
Logger.log("User provided: " + numericValue);
// Validate numeric input
function isValidInteger(input) {
return !Number.isNaN(Number.parseInt(input, 10));
}
Logger.log(isValidInteger("42")); // true
Logger.log(isValidInteger("A42")); // false
}
Output:
42.0
RGB: 51, 102, 255
User provided: 42
true
false
Number.parseInt() vs global parseInt()
In modern JavaScript, Number.parseInt()
and the global parseInt()
function are identical. The main reasons to use Number.parseInt() instead of the global parseInt() are:
Consistency with other Number methods
Makes it clear you're performing a number conversion
May be preferred in code style guides
Both functions have identical functionality, parameters, and return values.
Common Use Cases
The Number.parseInt()
method is useful in many scenarios when working with Google Apps Script:
Form data validation: Convert and validate numeric input from Google Forms or HTML forms.
Spreadsheet manipulation: Extract numeric portions from cell references or values.
Configuration settings: Parse numeric settings from string-based configuration values.
Color manipulation: Convert between hex color codes and RGB values when working with colors and visualizations.
String manipulation: Extract numeric portions from strings that mix text and numbers.
Mathematical operations: Ensure values are treated as integers rather than performing string concatenation.
Conclusion
In this tutorial, you learned how to use the Number.parseInt()
method to convert strings to integers in Google Apps Script. It's useful for many number parsing tasks. Always specify the radix parameter (usually 10 for decimal) to avoid unexpected behavior, and remember to handle potentially invalid inputs.