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
如何在不将数字转换为一个字符串的情况下将一数字转换为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

"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的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- HTML 学习笔记 JavaScript (Math和Number对象)
标签: Math对象:数学对象,提供对数据的数学计算.如:获取绝对值,向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. Number对象:Js中提供的数字的对象.包含整数,浮点数等等.并提 ...
- geeksforgeeks@ Largest Number formed from an Array
http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...
- ExtJS学习-----------Ext.Number,ExtJS对javascript中的Number的扩展
关于ExtJS对javascript中的Number的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...
- JavaScript 基本类型值-Number类型
▓▓▓▓▓▓ 大致介绍 在JavaScript的内部采用IEEE754格式来表示数字,所以不区分整数和浮点数,都是用64位浮点数的形式储存.就是说,在JavaScript内部,就根本没有小数.但是有些 ...
- JavaScript Math和Number对象研究
1. Math 对象 1.1 介绍 Math 对象,是数学对象,提供对数据的数学计算,如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 1.2 构造函数 无 : ...
- [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 ...
- JavaScript基础对象---Number
一.创建Number实例对象 /** * new Number(value); * value 被创建对象的数字值 * * Number 对象主要用于: 如果参数无法被转换为数字,则返回 NaN. 在 ...
- 在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 版权声明:本文 ...
随机推荐
- 对于Spring MVC 拦截器的一些了解
Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission lo ...
- 倍增小结 ST 与 LCA
倍增 倍增我是真滴不会 倍增法(英语:binary lifting),顾名思义就是翻倍. 能够使线性的处理转化为对数级的处理,大大地优化时间复杂度. (ps:上次学倍增LCA,没学会,老老实实为了严格 ...
- Typora使用与GItHhub图床配置
Typora使用 (windows) 1 快捷键 1.1 表格 快捷方式:CTRL+T ID name year 1 Oracle 10 2 Mysql 10 3 Postgresql 20 1.2 ...
- Java中运行javascript代码
Java中运行javascript代码 1.Java 代码 2.JS代码 2.1demoWithParams.js 2.2demoWithListParams.js 原文作者:russle 原文地址: ...
- ScalikeJDBC,操作mysql数据,API
ScalikeJDBC,操作mysql数据,API 一.构建maven项目,添加pom.xml依赖 二.resource文件下创建application.conf文件,并配置以下内容 三.操作mysq ...
- SparkStreaming算子操作,Output操作
SparkStreaming练习之StreamingTest,UpdateStateByKey,WindowOperator 一.SparkStreaming算子操作 1.1 foreachRDD 1 ...
- python flask_sqlalchemy 多态 polymorphic 实现单表继承
sqlalchemy 多态 polymorphic 实现单表继承 sqlaclchemy中的单表继续就是以一个模型类为基类,其他模型类继承基类,所有模型类的的数据都存一张表里面(也可以是多张,只不过基 ...
- spark sql优化
1.内存优化 1.1.RDD RDD默认cache仅使用内存 可以看到使用默认cache时,四个分区只在内存中缓存了3个分区,4.4G的数据 使用kryo序列化+MEMORY_ONLY_SER 可以看 ...
- Linux远程拷贝scp
Linux的scp命令可以实现两台服务器之间互相拷贝文件,我的测试环境是Centos6.4. 基本的命令格式 scp 拷贝目标文件 远程用户@远程主机地址:远程目录 一.从本机拷贝到目标远程主机 # ...
- 《Proxy系列专题》:代理模式(静态、JDK、CGLib)
<Proxy系列专题>:代理模式(静态.JDK.CGLib)使用 现象:在如今互联网时代,项目的复杂度不断的提升,有些场景下需要一定的设计优化来支撑业务的扩展,如为了不改动原始类,但需要对 ...