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