Example in my case:
d3.format("~s")(656628); //656.628k
But my expected result: 656.63k
I would like used option of d3-format instead of handling it manually.
Live test: https://npm.runkit.com/d3-format
All you need is d3.formatPrefix
, which...
Convert values to the units of the appropriate SI prefix for the specified numeric reference value before formatting in fixed point notation.
..., with ".2"
as the specifier and your own number as the value.
Here is the demo with your number:
const myNumber = 656628;
console.log(d3.formatPrefix(".2", myNumber)(myNumber));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
That gives you your expected result: 656.63k
.
And, for completeness, some other numbers as well:
[1232, 7676546655, 665, 0.0012, 8887878].forEach(function(d) {
console.log("Number: " + d + " - Your format: " + d3.formatPrefix(".2", d)(d))
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With