Category: FUNCTION | Updated: 2026-11-22

How to Use the DEC2HEX Function in Google Sheets | Convert Decimal to Hexadecimal

Syntax

=DEC2HEX(decimal_number, [significant_digits])
  • decimal_number: Specifies the decimal integer (-549,755,813,888 to 549,755,813,887) to convert to hexadecimal.
  • significant_digits (optional): Specifies the number of characters (1 to 10) for the output hexadecimal string. Leading zeros are padded on the left if needed.

Practical Example with a Table

Here is an example of converting decimal numbers to hexadecimal using the DEC2HEX function, including zero-padding with significant_digits and negative numbers.

Input (Decimal) Significant Digits Formula Result (Hexadecimal) Description
255 None =DEC2HEX(A2) FF Hexadecimal representation of 255
10 2 =DEC2HEX(A3, B3) 0A Padded with leading zero to 2 digits
4095 None =DEC2HEX(A4) FFF Maximum 3-digit hexadecimal value
-1 None =DEC2HEX(A5) FFFFFFFFFF Negative numbers always output as 10-digit two’s complement

Common Errors and Notes

  • #NUM! Error: Occurs if the number exceeds the convertible range (approx. ±549.7 billion), or if significant_digits is smaller than the number of digits required by the result.
  • Truncation of Decimals: If you pass a decimal fraction for decimal_number, the fractional part is truncated and it is converted as an integer.
  • Significant Digits for Negative Numbers: If decimal_number is negative, significant_digits is ignored and a 10-character string is always returned.

Frequently Asked Questions

Q. How do I create a color code from RGB values (0–255) using the DEC2HEX function?

A. Convert each R, G, and B value to a 2-digit hex string with `DEC2HEX(value, 2)` and concatenate them as `="#" & DEC2HEX(R,2) & DEC2HEX(G,2) & DEC2HEX(B,2)`.

Q. Can I output the hexadecimal letters in lowercase?

A. DEC2HEX always outputs uppercase letters (A–F). If you want lowercase, combine it with the LOWER function, such as `=LOWER(DEC2HEX(A1))`.