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 发布文章使用:只允许注册用户才可以访问!


how to convert a number to a number array in javascript without convert number to a string的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. HTML 学习笔记 JavaScript (Math和Number对象)

    标签: Math对象:数学对象,提供对数据的数学计算.如:获取绝对值,向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. Number对象:Js中提供的数字的对象.包含整数,浮点数等等.并提 ...

  3. geeksforgeeks@ Largest Number formed from an Array

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...

  4. ExtJS学习-----------Ext.Number,ExtJS对javascript中的Number的扩展

    关于ExtJS对javascript中的Number的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...

  5. JavaScript 基本类型值-Number类型

    ▓▓▓▓▓▓ 大致介绍 在JavaScript的内部采用IEEE754格式来表示数字,所以不区分整数和浮点数,都是用64位浮点数的形式储存.就是说,在JavaScript内部,就根本没有小数.但是有些 ...

  6. JavaScript Math和Number对象研究

    1. Math 对象 1.1 介绍   Math 对象,是数学对象,提供对数据的数学计算,如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法.   1.2 构造函数   无 : ...

  7. [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search

    Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...

  8. JavaScript基础对象---Number

    一.创建Number实例对象 /** * new Number(value); * value 被创建对象的数字值 * * Number 对象主要用于: 如果参数无法被转换为数字,则返回 NaN. 在 ...

  9. 在SQL中number(16,5)中的16和5 及number( 5,-2)中的5和-2是什么意思?

    在SQL中number(16,5)中的16和5 及number( 5,-2)中的5和-2是什么意思? 2018-06-04 19:23:24 xiaonan_IT 阅读数 3672   版权声明:本文 ...

随机推荐

  1. Bitter.Core系列 十二 :Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore 之 支持的 where 条件表达式

    Bitter.Core 内置了支持大部分的 linq 的条件表达式.基本上符合我们 where 条件所需.Bitter.Core 的支持 表达式内置  扩展 有: EndsWith    在Sql 解 ...

  2. 美团配送A/B评估体系建设与实践

    https://mp.weixin.qq.com/s/v3Fvp6Hed7ZGoE8FGlGMvQ

  3. 避免重复提交?分布式服务的幂等性设计! 架构文摘 今天 点击蓝色“架构文摘”关注我哟 加个“星标”,每天上午 09:25,干货推送! 来源:https://www.cnblogs.com/QG-whz/p/10372458.html 作者:melonstreet

    避免重复提交?分布式服务的幂等性设计! 架构文摘  今天 点击蓝色"架构文摘"关注我哟    加个"星标",每天上午 09:25,干货推送!      来源:h ...

  4. 【Python网络编程】epoll用法

    epoll发展进程 此处添加一下select.poll历程及其优缺点 原理 使用步骤 Create an epoll object--创建1个epoll对象 Tell the epoll object ...

  5. 为什么Redis集群要使用反向代理?

    为什么要使用反向代理? 如果没有方向代理,一台Redis可能需要跟很多个客户端连接: 看着是不是很慌?看没关系,主要是连接需要消耗线程资源,没有代理的话,Redis要将很大一部分的资源用在与客户端建立 ...

  6. CF492B

    题意 一条长为L的路,在n个不同的位置都放置了路灯,灯光半径相同,问半径至少为多少时灯光可以覆盖整条路. 那我们就先排序,使灯的位置是从路的一边依次排到另一边的 ,然后求出两两挨着的灯之间距离的最大值 ...

  7. (五)SpringBoot面试题

    SpringBoot面试题 1.Spring Boot 的自动配置是如何实现的? 2.shiro和oauth还有cas他们之间的关系是什么?问下您公司权限是如何设计,还有就是这几个概念的区别. 2.1 ...

  8. Go语言学习笔记(1)——顺序编程

    Go语言学习笔记这一堆主要是<Go语言编程>(人民邮电出版社)的读书笔记.中间会穿插一些零碎的点,比如源码学习之类的.大概就是这样吧. 1. 顺序编程 1.1 变量 变量的声明: var ...

  9. GeoMesa Java API-写入与查询数据

    GeoMesa Java API-写入与查询数据 写入数据 DataStore SimpleFeatureType SimpleFeature 写入 查询数据 几个常用查询条件 设置最大返回条目: 设 ...

  10. SANGFOR AC配置AD域单点登录(二)----AD域侧配置及单点登录认证、注销测试

    1.AD域侧配置 1)新建组策略并配置logon登录脚本,以实现用户开机登录域时,自动通过AC认证  AD域服务器"运行"输入gpmc.msc,打开组策略编辑器,如下图. 右建需要 ...