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

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

Number To Array

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://stackoverflow.com/questions/63061316/how-to-convert-a-number-to-a-number-array-in-javascript-without-convert-number-t

"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 stackoverflow



xgqfrms 2012-2020

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


最新文章

  1. HTML5_01之表单新特性
  2. Java线程
  3. c#修改config中的AppSettings属性
  4. js清除cookie
  5. objective-c系列-NSArray
  6. servlet部分知识总结
  7. 【转】Unity3D中Layers和LayerMask解析
  8. 8月11日 Power-BI V11 QQ群视频交流开课啦
  9. ubuntu下安装 Source insight
  10. c++primer复习(六)—面向对象编程
  11. SharePoint用户控件编写的简单介绍
  12. hdu 4336 Card Collector(期望 dp 状态压缩)
  13. Servlet起步
  14. unity零基础开始学习做游戏(一)为了实现你的游戏,你需要提前做的准备工作
  15. 从0开始的Python学习008变量
  16. 走过的easyui的坑--datagrid的reload在IE下未刷新
  17. 简单两步 ~ 绕过 "Paused in debugger"
  18. vs变量监视提示-VAR-CREATE: UNABLE TO CREATE VARIABLE OBJECT解决方法
  19. jquery 强大表格插件 DataTables
  20. Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)

热门文章

  1. protoc-gen-validate (PGV)
  2. 京东零售mockRpc实践
  3. ctsc选课
  4. Git轻松入门1:本地仓库篇
  5. Ceph对象存储 S3
  6. 数据库MySQL——SQL语句(命令)
  7. python--可迭代对象、迭代器和生成器
  8. 一个简单且易上手的 Spring boot 后台管理框架-->EL-ADMIN
  9. 2019牛客多校 Round8
  10. 【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)