在我的网站上,我有一个具有大量数字的表,我对它们进行了简单的数学,为用户找到了最佳方法。桌子看起来像这样:
US
locale
+-------+-------+------------+---------+----------------------------------------+
| Gain | Price | Price/Gain | Amount | Outcome (round(Price * Amount)) |
+-------+-------+------------+---------+----------------------------------------+
| 15.75 | 47 | 2.98 | 827,583 | 38,896,401 |
| 52.5 | 240 | 4.57 | 248,275 | 59,586,000 |
| 297.5 | 4,106 | 13.80 | 43,814 | 179,900,284 |
+-------+-------+------------+---------+----------------------------------------+
这是de-DE
语言环境的外观(请注意。和字符的切换(
+-------+-------+------------+---------+----------------------------------------+
| Gain | Price | Price/Gain | Amount | Outcome (round(Price * Amount)) |
+-------+-------+------------+---------+----------------------------------------+
| 15,75 | 47 | 2,98 | 827.583 | 38.896.401 |
| 52,5 | 240 | 4,57 | 248.275 | 59.586.000 |
| 297,5 | 4.106 | 13,80 | 43.814 | 179.900.284 |
+-------+-------+------------+---------+----------------------------------------+
我的代码工作方式,每列分别填充。因此,首先,Gain
列是针对所有行填充的,格式化,并使用jQuery
,我将row n
的值更改为Gain
。然后计算Price
,然后再次填充值。
计算Price/Gain
时会出现问题。对于US
语言环境,一切都很好。但是使用de-DE
语言环境,表实际上看起来像这样:
+-------+-------+------------+---------+----------------------------------------+
| Gain | Price | Price/Gain | Amount | Outcome (round(Price * Amount)) |
+-------+-------+------------+---------+----------------------------------------+
| 15,75 | 47 | 0,03 | 827.583 | 38.896.401 |
| 52,5 | 240 | 0,46 | 248.275 | 59.586.000 |
| 297,5 | 4.106 | 0,00 | 43.814 | 179.900 |
+-------+-------+------------+---------+----------------------------------------+
计算Price/Gain
时,,
和.
被Gain
和Price
列忽略。正如您在第三行中看到的那样,
4.106 / 2975 = 0.0014 (rounded to 0.00, or 0,00)
这也引起了Outcome
的问题,因为Price
再次被从字面上解析,而不是首先从de-DE
转换为我们。因此,在第三行,我们再次可以看到
4.106 * 43814 (this is parsed correctly for some reason) = 179,900 or 179.900
这是我的代码在需要时发布这些值并在需要时阅读它们:
// This code populates the Gain field
var gain = grabPresetGain(); // grabs a hardcoded value from a JSON object
gain = addCommasToNumber(new Intl.NumberFormat("de-DE", {}).format(gain .toFixed(2)));
row += "<div class='col-lg-1 col-md-1 col-sm-1 col-xs-1 row-gain text-center'>" + gain + "</div>";
// This code populates the price field
outcome = addCommasToNumber(new Intl.NumberFormat("de-DE", {}).format(outcome));
$(this).children(".row-price").text(outcome);
// And finally this code populates the Price/Gain field
// for loop going through each row of the table
var gain = convertToNumber($(this).children(".row-gain").text());
var price = convertToNumber($(this).children(".row-price").text());
var pricePerGain = (price / gain);
pricePerGain = addCommasToNumber(new Intl.NumberFormat("de-DE", {minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(pricePerGain .toFixed(2)));
$(this).children(".row-pricegain").text(pricePerGain );
这是使用的两个辅助功能:
function convertToNumber(x) {
if (typeof x == "string") {
x = x.replace(/,/g,'');
}
return Number(x);
}
function addCommasToNumber(n) {
return n.toLocaleString();
}
总的来说 - 是否有一种一致的方法来解析,操纵和输出数字?
任何洞察力都会很棒,因为我目前该怎么办。
谢谢
尝试格式化。从网站:
格式是用于国际化的JavaScript库的模块化集合,专注于格式化数字,日期和字符串,以显示向人们展示。
如果您只是处理数字,您甚至可以完全不使用库,而只能直接访问window.Intl
对象。
在MDN上,请参见一些示例。