how to convert a number to a number array in javascript without convert number to a string

时间:2021-12-23 14:36:48

how to convert a number to a number array in javascript without convert number to a string

如何在不将数字转换为一个字符串的情况下将一数字转换为javascript中的一个数字数组

Number To Array

how to convert a number to a number array in javascript without convert number to a string

Math.round 四舍五入 bug


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-23
* @modified
*
* @description 9. Palindrome Number
* @difficulty Easy
* @complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/palindrome-number/
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
* @solutions
*
*/ const log = console.log; function NumberToArray(num = 1) {
const result = [];
const len = Math.ceil(Math.log10(num + 1));
let temp = num;
for (let i = len; i > 0; i--) {
log(`\ntemp 1`, temp, len)
// Math.round 四舍五入 bug
log(`value =`, parseInt(temp / Math.pow(10, i - 1)), Math.pow(10, i - 1))
result.push(parseInt(temp / Math.pow(10, i - 1)));
log(`result[len - i]`, result[len - i], len - i, len, i)
log(`result[len - i] * Math.pow(10, i - 1)`, result[len - i] * Math.pow(10, i - 1))
temp -= result[len - i] * Math.pow(10, i - 1);
log(`temp`, temp)
}
// for (let i = len; i > 0; i--) {
// log(`\ntemp 1`, temp, len)
// // Math.round 四舍五入 bug
// log(`value =`, Math.round(temp / Math.pow(10, i - 1)), Math.pow(10, i - 1))
// result.push(Math.round(temp / Math.pow(10, i - 1)));
// log(`result[len - i]`, result[len - i], len - i, len, i)
// log(`result[len - i] * Math.pow(10, i - 1)`, result[len - i] * Math.pow(10, i - 1))
// temp -= result[len - i] * Math.pow(10, i - 1);
// log(`temp`, temp)
// }
// for (let i = len; i > 0; i--) {
// log(`num % (i * 10)`, parseInt(num / Math.pow(10, i - 1)), Math.pow(10, i - 1))
// result.push(parseInt(num / Math.pow(10, i - 1)));
// }
return result;
} // const test = NumberToArray(123);
// const test = NumberToArray(1234);
// const test = NumberToArray(12345);
const test = NumberToArray(1234567); log(`test`, test)

refs

https://*.com/questions/63061316/how-to-convert-a-number-to-a-number-array-in-javascript-without-convert-number-t

how to convert a number to a number array in javascript without convert number to a string

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-23
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/ const log = console.log; const NumberToNumberArray = (num = 1) => {
const result = [];
const len = Math.ceil(Math.log10(num + 1));
let temp = num;
for (let i = len; i > 0; i--) {
result.push(parseInt(temp / Math.pow(10, i - 1)));
temp -= result[len - i] * Math.pow(10, i - 1);
}
return result;
} // const test = NumberToNumberArray(123);
// const test = NumberToNumberArray(1234);
// const test = NumberToNumberArray(12345);
const test = NumberToNumberArray(1234567); log(`test`, test)
// [1, 2, 3, 4, 5, 6, 7]

holy shit *

how to convert a number to a number array in javascript without convert number to a string

how to convert a number to a number array in javascript without convert number to a string


how to convert a number to a number array in javascript without convert number to a string

xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!