【问题标题】:Getting place values of a number w/ modulus?获取带模数的数字的位置值?
【发布时间】:2014-08-05 06:04:20
【问题描述】:
  • 我需要获取用户提交的随机数的位值。 这个数字可以是 0-1000000000000000 (零到一 万亿)

  • 我认为这可以通过使用JavaScript模数%来实现 操作员。问题,我真的不知道怎么用,我也不知道 明白了。

  • 这里是Fiddle

(我只知道10%3 返回1,因为3*3 = 910-9 = 1

我算出了个数、十、百和千:

var ones = Math.floor(num % 10),
    tens = Math.floor(num / 10 % 10),
    hundreds = Math.floor(num / 100 % 10),
    thousands = Math.floor(num % 10000 / 1000);

只是需要:

  1. 一万
  2. 数十万
  3. 百万
  4. 一千万
  5. 数亿
  6. 十亿
  7. 百亿
  8. 千亿
  9. 万亿

-- 我什至不知道其余的是否可能,但是如果您有任何提示或至少找到一个提示,请告诉我!谢谢。

【问题讨论】:

  • "这个问题,我真的不知道怎么用,也不明白。" -- 见这里en.wikipedia.org/wiki/Modular_arithmetic
  • num.toString(10).split("")?你需要什么地方值?
  • 我正在做一个数字到单词的翻译器。
  • @Bergi -- 这很有帮助。谢谢!
  • @CptRobby -- 感谢您的帮助。如果您在下面查看我的答案,您会发现我想通了!是的,这就是我正在做的,但用的是西班牙语。 :)

标签: javascript math division modulus


【解决方案1】:
        var ones = Math.floor(num % 10),
            tens = Math.floor(num/10 % 10),
            hundreds = Math.floor(num/100 % 10),
            thousands = Math.floor(num/1000 % 10),
            tenThousands = Math.floor(num / 10000 % 10),
            hundredThousands = Math.floor(num / 100000 % 10),
            millions = Math.floor(num / 1000000 % 10),
            tenMillions = Math.floor(num / 10000000 % 10),
            hundredMillions = Math.floor(num / 100000000 % 10);

设法达到 1 亿。

【讨论】:

  • 我无法编辑,但我相信第四行应该是:thousands = Math.floor(num / 1000 % 10)
【解决方案2】:
var scaleStepWidth = 0;
var number = Math.ceil(1052220420.32);
var placeValue = 1;
for(i=0;i<(number.toString().length-1);i++){
    placeValue *= 10;
}
scaleStepWidth = eval(number.toString()[0])*placeValue;

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 2021-05-30
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    相关资源
    最近更新 更多