- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现很多资源都在讨论基数转换算法和使用内置 Javascript 函数将十进制转换为二进制或十六进制,但是我找不到任何关于如何转换为任何小数基数(例如基数 3/2)的资源. sesquinary 数字系统非常有趣,我希望能够使用它,但我找不到任何方法将其从 Decimal 转换为它。
半数系统如下:1、2、20、21、22、210、211、212、2100、2101等
我找不到任何可靠的方法来转换它。这是我到目前为止的代码:
function decimalToBase(number, base) {
let digitArray = [];
let quotient;
let remainder;
for (let i = 0; i < 16; i++) {
quotient = Math.floor(number / base);
remainder = number % base;
digitArray.unshift(remainder);
number = quotient;
if (quotient == 0){
break;
}
}
console.log(digitArray)
}
它适用于 10 以下的整数基数,但如果我输入像 3/2 这样的小数基数,那么我会得到带小数的结果:
[1, 0.5, 1, 0] // When It should be: [2, 1, 0]
如有任何帮助,我们将不胜感激。
最佳答案
对于 3/2
特定情况,this article詹姆斯·普罗普 (James Propp) 公开了几种方法,其中一种是西蒙·诺顿 (Simon Norton) 在尾注中描述的:
It’s based on the fact that when n is written as 3k + r, where the remainder r is 0, 1, or 2, the sesquinary representation of n is equal to the sesquinary representation of 2k, with the digit r tacked on at the end. This gives us a quick way to write the sesquinary representation of n from right to left via a process of repeatedly dividing by three, rounding down, and doubling.
他们还提供了一些 Mathematica 代码来运行它。
S[n_] := If[n < 3, {n}, Append[S[2 Floor[n/3]], Mod[n, 3]]]
我真的不懂Mathematica,所以我不能真正用js一对一重写它,但如果我正确理解算法,那将是这样的:
const decimalToSesquinary = (n) => {
const res = [];
while (n) {
const k = Math.floor(n / 3);
res.push(n % 3);
n = k * 2;
}
return res.reverse().join("");
};
console.log(decimalToSesquinary(6)) // expected "210"
console.log(decimalToSesquinary(7)) // expected "211"
console.log(decimalToSesquinary(8)) // expected "212"
console.log(decimalToSesquinary(100)) // expected "212001201"
关于javascript - 如何在 Javascript 中将小数(基数 10)转换为小数基数(即基数 3/2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63838420/
如何将整数类型转换为 double /浮点类型以显示小数点?例如,如果我想将数字转换为货币格式: 5 会变成 5.004.3 会变成 4.30 javascript 有什么东西可以用来做这种转换吗?
【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/cnb-yuchen/p/18107586 出自【进步*于辰的博客】
我意识到这是一个重复的问题,但是 this 中提到的解决方案这个问题完全不适合我。 我目前的代码如下 Sub ConvertTextToNumber() Dim Area As Range, C As
我正在使用数学 javascript,但在用它来替换点的逗号和逗号的点时遇到了一些麻烦。我可以改变千位分隔符的逗号,但无法设法将小数点变成逗号。我尝试了其他帖子中的一些建议,但没有感到高兴。目标是实现
我正在尝试在 Android 中创建一个数字选择器,但轮子只增加 1。我想增加 0.1。我在网上查了一下,但我发现了一个格式化的浮点数组禁用了轮子。请帮助并为语法感到抱歉,我正在学习。 最佳答案 您可
我正在尝试在多个网站上获取利率。数据相当非结构化,但形式足够接近。我想要捕捉的内容: x.xx% 至 xx.xx% 数据示例: 由 FDIC 成员 WebBank 发放的所有贷款。您的实际利率取决于信
在 MySQL 表中,我有一个具有不同值的 VARCHAR 列,这些值可能代表字符串、整数、浮点、任意值。这些值作为特定于语言的字符串写入数据库,这意味着 123.45 的浮点值可以写为德语中的 "1
我想编写一个正则表达式,它允许整数或具有 0 - 2 个小数位的小数。 有效输入 1 1. 1.1 1.11 111111111 111111111. 111111111.1 111111111.11
我正在尝试为 nullable 实现客户端验证其小数点分隔符可以是逗号(例如:123,45)。 在我看来: ... @Html.LabelFor(model => model.Turnove
我找不到合适的正则表达式来仅从字符串中提取 float 。考虑以下字符串: $string = "8x2.1 3x2"; 我想提取 2.1,我尝试了以下操作,但这给了我整数和 float : preg
我希望使用正则表达式函数分离以下数据,如下所示: 要使用的功能: let fx=(text,regex)=> Web.Page( " var x='
我是 jquery 新手。我有一个带有两个输入框的表单。我实现了一些验证。 Min.Amount Max.Amount
我正在java中实现一个简单的算法,它接受一个整数数组,并查找并返回数组中相邻整数的最大乘积。 为此,我首先初始化了一个名为largestProduct的变量,我用它来跟踪当前找到的最大(最佳)产品。
在 JavaScript 中,我想定义小数点的位置。我只能在示例中真正展示它。 假设输入值为 1234 。 我希望输出为 123.4 。 或者,如果输入是 12345 ,我希望输出是 123.45 。
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我目前正在开发一些基于 Django 的 Web 项目,在这个 Web 开发过程中,我遇到了以下我无法正确理解的代码。 if price_product['price'] == Decimal('-1
这个问题在这里已经有了答案: How do I print a double value with full precision using cout? (17 个答案) 关闭 7 年前。 我试图在
这应该是微不足道的,但我正在兜圈子,也许有人可以提供帮助。 我有两个表(T1,T2),我希望从中提取每行中的多个值并更新第三个表(T3)的内容当且仅当)T1 中有两个 UQ,NN 字段,T2 匹配,在
如果数字不是十进制,我需要附加.00,但是当我尝试下面的代码时,它会将整个数字更改为0.00。例如,如果数字是12,200,它会将其更改为0.00,而不是在末尾添加.00 $('.total-amou
我正在尝试在容器 View 中设置 9:16 纵横比 View 。以下代码在 viewDidLayoutSubviews 中设置约束,以便在正确的位置考虑自动布局。它还调用 layoutIfNeede
我是一名优秀的程序员,十分优秀!