The Number object's toString() method
The toString()
method returns a string representing the specified Number object.
Prerequisites
This tutorial assumes that you're familiar with:
SyntaxnumObj.toString([radix])
Parametersradix (optional):
An integer between 2 and 36 specifying the base to use for representing the number value
Default is 10 (decimal) if not specified
Allows conversion to binary (2), octal (8), hexadecimal (16), or other number systems
Return value
numObj.toString([radix])
radix (optional):
An integer between 2 and 36 specifying the base to use for representing the number value
Default is 10 (decimal) if not specified
Allows conversion to binary (2), octal (8), hexadecimal (16), or other number systems
The toString()
method returns a string representing the specified Number value.
ExamplesExample 1: Basic number to string conversion
In the code below, the function toStringEx1()
demonstrates basic number-to-string conversion.
function toStringEx1() {
let num = 42;
// Basic conversion to string
Logger.log(num.toString()); // "42"
Logger.log((3.14159).toString()); // "3.14159"
// String concatenation with numbers
Logger.log("The answer is " + num); // "The answer is 42" (implicit toString())
// Explicit vs. implicit conversion
Logger.log(num.toString() === num + ""); // true
// Using toString() on numeric literals requires parentheses
// Logger.log(42.toString()); // This would cause a syntax error
Logger.log((42).toString()); // "42" (correct syntax)
// Negative numbers
Logger.log((-123).toString()); // "-123"
}
Output:
42
3.14159
The answer is 42
true
42
-123
Example 2: Using different radices
In the code below, the function toStringEx2()
shows how to convert numbers to strings using different number bases.
function toStringEx2() {
let num = 42;
// Decimal (base 10) - default
Logger.log(num.toString(10)); // "42"
// Binary (base 2)
Logger.log(num.toString(2)); // "101010"
// Octal (base 8)
Logger.log(num.toString(8)); // "52"
// Hexadecimal (base 16)
Logger.log(num.toString(16)); // "2a"
// Other bases
Logger.log(num.toString(36)); // "16" (highest supported base: 0-9, a-z)
}
Output:
42
101010
52
2a
16
Example 3: Handling special cases
In the code below, the function toStringEx3()
demonstrates handling of special numeric values.
function toStringEx3() {
// Special numeric values
Logger.log(NaN.toString()); // "NaN"
Logger.log(Infinity.toString()); // "Infinity"
Logger.log((-Infinity).toString()); // "-Infinity"
// Very large or small numbers
Logger.log((1e21).toString()); // "1e+21" (scientific notation)
Logger.log((1e-8).toString()); // "1e-8"
// Zero values
Logger.log((0).toString()); // "0"
Logger.log((-0).toString()); // "0" (negative zero becomes "0")
// Floating point precision
Logger.log((0.1 + 0.2).toString()); // "0.30000000000000004" (floating point issue)
}
Output:
NaN
Infinity
-Infinity
1e+21
1e-8
0
0
0.30000000000000004
Example 4: Practical applications in Apps Script
In the code below, the function toStringEx4()
demonstrates practical uses of toString()
in Google Apps Script.
function toStringEx4() {
// Formatting spreadsheet row/column numbers
let columnNumber = 27;
Logger.log("Column " + columnNumber + " in A1 notation: " +
getColumnLetter(columnNumber)); // "AA"
// Converting numbers to hex for color codes
let red = 255;
let green = 128;
let blue = 0;
let hexColor = "#" + componentToHex(red) + componentToHex(green) + componentToHex(blue);
Logger.log("RGB(" + red + "," + green + "," + blue + ") as hex: " + hexColor); // "#ff8000"
// Helper function to convert spreadsheet column number to letter
function getColumnLetter(column) {
let temp, letter = '';
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
// Helper function to convert a number component to a 2-digit hex value
function componentToHex(c) {
let hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
}
Output:
Column 27 in A1 notation: AA
RGB(255,128,0) as hex: #ff8000
Example 5: Working with fixed precision and formatting
In the code below, the function toStringEx5()
demonstrates how to use toString()
with formatting numbers for display.
function toStringEx5() {
let value = 42.1234;
// Basic toString
Logger.log(value.toString()); // "42.1234"
// toString() vs toFixed() for decimal precision
Logger.log(value.toString()); // "42.1234" (full precision)
Logger.log(value.toFixed(2)); // "42.12" (fixed decimal places)
// Formatting large numbers with thousand separators
let largeNum = 1234567.89;
Logger.log(formatNumber(largeNum)); // "1,234,567.89"
// Helper function to format number with thousand separators
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
Output:
42.1234
42.1234
42.12
1,234,567.89
toString() vs String() vs Concatenation
There are multiple ways to convert numbers to strings in JavaScript. Here's how they compare:
function compareStringConversions() {
let num = 42;
// Method 1: toString() method
Logger.log(num.toString()); // "42"
// Method 2: String() constructor
Logger.log(String(num)); // "42"
// Method 3: Concatenation with empty string
Logger.log(num + ""); // "42"
// Key differences with special values
Logger.log(String(null)); // "null"
// Logger.log(null.toString()); // Error: Cannot read property 'toString' of null
Logger.log(String(undefined)); // "undefined"
// Logger.log(undefined.toString()); // Error: Cannot read property 'toString' of undefined
}
Output:
42
42
42
null
undefined
Common Use Cases
The toString()
method for numbers is useful in many scenarios when working with Google Apps Script:
Data formatting: Converting numeric values to strings for display or reporting.
Color manipulation: Converting RGB values to hexadecimal color codes.
Spreadsheet references: Converting row/column numbers to A1 notation.
Binary operations: Visualizing bit flags or binary data.
Custom number formatting: Creating formatted strings for currency, percentages, or other numeric representations.
Base conversion: Converting between different number systems for specialized applications.
Best Practices
When using the toString() method with numbers, keep these tips in mind:
Remember to use parentheses when calling
toString()
on numeric literals:(42).toString()
, not42.toString()
.Consider using
toFixed()
for controlling decimal places when formatting floating-point numbers.Use
String()
constructor instead oftoString()
when dealing with values that might be null or undefined.For complex formatting needs, consider combining
toString()
with string methods likepadStart()
,replace()
, etc.
Conclusion
In this tutorial, you learned how to use the toString()
method to convert numbers to strings in Google Apps Script. You can use toString()
to format numeric data for display, perform base conversions, and create custom string representations of numbers in your Apps Script projects.
Small Scripts, Big Impact
Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation
By subscribing, you agree to our Privacy Policy and Terms of Service