javascriptcsstypescripthex

Typescript: Convert from hex number to string for CSS hex color


I have the following code:

  const myNumber: number = 0xFFFFFF;

How can I convert myNumber to '#FFFFFF'?


Solution

  • Just use toString() method and a #

    const myNumber: number = 0xFFFFFF;
    const hexString: string = '#' + myNumber.toString(16);  // '#ffffff'