【发布时间】:2021-05-31 13:34:06
【问题描述】:
我正在尝试在 D3.js 中添加自定义千位分隔符和十进制字符 我按照以下链接进行操作,但该解决方案适用于 3.4.11 版,但我希望将其用于 5.7.0 版: How to add a (custom) thousand separator in D3?
下面是代码:
// custom localization options
var myLocale = {
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["$", ""]
}
// create custom locale formatter from the given locale options
const localeFormatter = d3.locale(myLocale);
const formater = ",.2f";
// create a formatter for the number (grouped thousands with two significant digits). By default ',' means 'thousands' but we switched that into a '.' in our custom localization
const numberFormat = localeFormatter.numberFormat(formater);
// test
//alert(numberFormat(10235.6789)); // "1.000.000"
//$("#number").html(numberFormat(10235.6789))
document.getElementById('number').innerHTML = numberFormat(10235.6789)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<p id="number">
</p>
【问题讨论】:
标签: javascript d3.js