Search code examples
javascriptnumberstrimdigits

Trim long number in JavaScript


For example, I got that long nubmer 1517778188788. How can i get first 6 digits from that number, like 151777 and trim another digits?


Solution

  • Just convert a number to string and then slice it and convert it back to Number.

    const a = 1517778188788;
    const str_a = a.toString();
    const result = Number(str_a.slice(0, 6));