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. IDEA 2019 Unable to get current time from Google's servers 解决

    取消android support即可

  2. ModuleNotFoundError 模块寻找路径

    t = os.path.dirname(os.path.dirname((os.path.dirname(os.path.abspath(__file__)))))os.path.sys.path.a ...

  3. 用tqdm和rich为固定路径和目标的python算法代码实现进度条

    适用场景 在存在固定长度的算法中可以可视化算法执行的过程,比如对一个固定长度的数组的遍历,就是一种适合使用进度条来进行可视化的场景.而一些条件循环,比如while循环,不一定适合使用进度条来对算法执行 ...

  4. 一个sql盲注小工具 (Golang版)

    并发,二分法判断. 源码写的有点垃圾,有点乱,结果也存在一些缺失. 记录: sql:select distinct 中的distinct选项,这是只会获取你表中不重复数据,是表中,而不是你一次sql执 ...

  5. JTS Geometry

    JTS Geometry关系判断和分析 JTS Geometry关系判断和分析 1.关系判断 1.1实例 2.关系分析 2.1实例 JTS(Geometry) JTS Geometry关系判断和分析 ...

  6. 封装各种生成唯一性ID算法的工具类

    /** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 ( ...

  7. 配置七牛云图床 + Typora

    配置七牛云图床工具 使用图床+Typora可以方便快捷的撰写图文博客 我这里以七牛云进行示例,讲解如何去配置 七牛云是属于收费图床,目前还在测试,不过对于使用量不大的我来说应该免费是足够了的,不过需要 ...

  8. Flink-v1.12官方网站翻译-P018-Event Time

    事件时间 在本节中,您将学习如何编写时间感知的Flink程序.请看一下及时流处理,了解及时流处理背后的概念. 关于如何在Flink程序中使用时间的信息请参考windowing和ProcessFunct ...

  9. three.js cannon.js物理引擎制作一个保龄球游戏

    关于cannon.js我们已经学习了一些知识,今天郭先生就使用已学的cannon.js物理引擎的知识配合three基础知识来做一个保龄球小游戏,效果如下图,在线案例请点击博客原文. 我们需要掌握的技能 ...

  10. Codeforces Round #646 (Div. 2) A. Odd Selection(数学)

    题目链接:https://codeforces.com/contest/1363/problem/A 题意 判断是否能从 $n$ 个数中选 $x$ 个数加起来和为奇数. 题解 首先 $n$ 个数中至少 ...