Logo

dev-resources.site

for different kinds of informations.

Get all separate digits of a number (TypeScript)

Published at
7/6/2022
Categories
typescript
numbers
math
Author
amarok24
Categories
3 categories in total
typescript
open
numbers
open
math
open
Author
8 person written this
amarok24
open
Get all separate digits of a number (TypeScript)

Do you need to "extract" each digit of a given number? Then this little TypeScript program is probably exactly what you're looking for. I have inserted some important comments about the "safe" input range, so make sure you are aware of it before using it in critical segments. There are many different solutions, this one avoids converting the number to a String internally, so it's a pure mathematical algorithm. (Note: I have updated the code to work correctly if given number is 0.)

/**
 * @param num A number in the int32 range [-2147483648 .. 2147483647]
 * @returns A number[] containing each digit of `num`.
 * If negative `num` given, resulting array will contain negatives numbers.
 */
function separateDigits(num: number): number[] {
  let arr: number[] = [];
  let lastDigit: number;

  num = toInt32(num);

  do {
    lastDigit = num % 10;
    arr.push(lastDigit);
    // Updating num to num/10 cuts off the last digit:
    num = toInt32(num / 10);
  }
  while (num !== 0);

  return arr.reverse();
}

/**
* Fast bitwise operation, truncates floating point number resulting in int32.
* The >> bitwise operator is used, an overflow occurs if number too large.
* A *safe* integer division in JavaScript would be Math.floor(x/y);
* @param f A (JavaScript) number between [-2147483648.999 .. 2147483647.999]
* @returns Input 'f' truncated to Int32.
*/
function toInt32(f: number): number {
  // Note that type "number" in JS is always "float" internally.
  return f >> 0;
}

console.log(separateDigits(3142520));

// Output:
// [3, 4, 1, 2, 5, 2, 0]
Enter fullscreen mode Exit fullscreen mode
numbers Article's
29 articles in total
Favicon
Leetcode โ€” 3289. The Two Sneaky Numbers of Digitville
Favicon
Master Number Conversions with Ease: From Binary to Hexadecimal & More!
Favicon
Leetcode โ€” 2769. Find the Maximum Achievable Number
Favicon
Leetcode โ€” 3190. Find Minimum Operations to Make All Elements Divisible by Three
Favicon
Floating Point Representation (IEEE 754 ISSUE)
Favicon
Error in Javascript
Favicon
Million day archivio
Favicon
Exploring the Mystical Language of Numbers.
Favicon
How to Check if a Value is Within a Range of Numbers in JavaScript
Favicon
BINARY NUMBERS
Favicon
Prime Numbers โ€“ Definition with Examples
Favicon
Generator Random Numbers
Favicon
Binary representation of numbers
Favicon
Get all separate digits of a number (TypeScript)
Favicon
The fascinating diagonals of Pascal's Triangle
Favicon
Convert long numbers to string
Favicon
Patterns in the multiples of 2, 5, 10 and 3
Favicon
Your startup & the conspiracy of numbers
Favicon
Teaching the computers to write number names using JavaScript
Favicon
Learn Bash by making a game
Favicon
Convert Decimal to Binary for IOT Product With Vue.js
Favicon
Hunt some strange numbers with Python.
Favicon
Generate unique (non-repeating) random numbers
Favicon
Developer 2021 - Projections based from Stack Overflow's 2020 Developer Survey
Favicon
Learn Python: Numbers
Favicon
Find Valid Phone Numbers, Anywhere!
Favicon
Quick vim tips to generate and increment numbers
Favicon
What is the type of NaN?
Favicon
Recreating Excelโ€™s ISNUMBER in a Numbers Spreadsheet

Featured ones: