简体   繁体   English

Javascript中模数运算符不准确

[英]Modulo operator innaccuracy with large numbers in Javascript

I am writing some code to convert large integers from a thousand-based system (eg 1,234,567,891) to the Japanese system of ten-thousand (eg 12億3456万7891). 我正在编写一些代码,将大整数从基于千的系统(例如1,234,567,891)转换为日语的万系统(例如120亿3456万7891)。 In short, writing out numbers to a readable format in Japanese means splitting numbers into sets of four instead of sets of three. 简而言之,用日语将数字写成可读格式意味着将数字分成4组而不是3组。

The user inputs a number (var innum ), and the modulo operator is used with divisors of 10,000's to pinpoint the 4-digit chunks where I would like the number marked with kanji (Chinese characters). 用户输入一个数字(var innum ),模运算符与10,000的除数一起使用,以查明我希望用汉字(汉字)标记的数字的4位数据块。

ex: where chobase =10^12 (one-trillion), okubase =10^8 (100-million) and manbase =10000 (ten-thousand), and say the user input is 54234238524003: 例如:其中chobase = 10 ^ 12(一万亿), okubase = 10 ^ 8(一亿)和manbase = 10000(一万),并说用户输入为54234238524003:

 var cho = Math.floor((innum/chobase)); //returns 54 var oku = Math.floor(((innum%chobase)/okubase)); //returns 2342 var man = Math.floor(((innum%okubase)/manbase)); //returns 3852 var sen = (innum%manbase); //returns 4003 

slap it all together as a string and... 54兆2342億3852万4003 像拍弦一样把它拍打起来... 54兆2342亿3852万4003
Excellent- it works great. 很棒-很棒。

However, when the integer tips into the ten-quadrillions (10^16), the modulo operator is no longer reliable. 但是,当整数切入十个四十进制数(10 ^ 16)时,模运算符将不再可靠。

example produced by code using the same logic as above (commas added for readability): 使用与上述相同的逻辑由代码产生的示例(为便于阅读,添加了逗号):

input: 4,234,322,423,423,424,003 输入:4,234,322,423,423,424,003
output: 423京4322兆4234億2342万4000 输出:423京4322兆4234亿2342万4000

This is incorrect, as the last four digits in the Japanese format should read 4003 given the input. 这是不正确的,因为在输入的情况下,日语格式的最后四位数字应为4003。

I realize that JavaScript numbers are floating point and that accuracy wanes as integers reach a certain size, so are there any suggestions on how to work around this for numbers of ten-quadrillions in size? 我意识到JavaScript数字是浮点数,并且随着整数达到一定大小,精度会下降,因此,对于十个四分位数的数字,有何建议解决此问题? (This is roughly the maximum size number I am aiming to convert). (这大约是我要转换的最大大小数)。 I saw php has an fmod() method for handling this type of situation, but I have been unable to find a javascript equivalent.. 我看到php有一个fmod()方法来处理这种情况,但我一直找不到对应的JavaScript。

I'd consider leaving them as strings if possible, then just using substring operations to manipulate them. 我会考虑尽可能将它们保留为字符串 ,然后仅使用子字符串操作来操纵它们。 Then you don't have to worry about whether they'll fit into a numeric data type. 然后,您不必担心它们是否适合数字数据类型。

By way of example, consider the following script: 通过示例,考虑以下脚本:

function makeNum(num) {
    num = num.replace(/,/g,"");
    var markers = "万億兆京";
    var result = "";
    while (num.length > 4) {
        if (markers.length == 0) {
            result = "(未詳)" + num.substr(num.length-4) + result;
        } else {
            result = markers.substr(0, 1) + num.substr(num.length-4) + result;
            markers = markers.substr(1);
        }
        num = num.substr(0, num.length-4);
    }
    return num + result;
}

alert(makeNum("4,234,322,423,423,424,003"));

This first removes all commas then builds up the Japanese number section by section, using markers to select the correct separators. 首先删除所有逗号,然后使用markers选择正确的分隔符,逐节构建日语数字。 If your number is so big that it runs out of markers, I just use (未詳) instead which, if it doesn't mean "unknown", you can blame on the Japaneses-English online dictionary I used. 如果您的电话号码太大,以致标记用完了,我就改用(未詳) ,如果它的意思不是 “未知”,您可以将其归咎于我使用的日英在线词典。

The result of that script is, as expected, a dialog box containing 423京4322兆4234億2342万4003 . 如预期的那样,该脚本的结果是一个对话框,其中包含423京4322兆4234億2342万4003

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM