I need to show a currency value in the format of 1K of equal to one thousand, or 1.1K, 1.2K, 1.9K etc, if its not an even thousands, otherwise if under a thousand, display normal 500, 100, 250 etc, using javascript to format the number?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
Here's a simple solution that avoids all the
if
statements (with the power ofMath
).Bonus Meme
What does
SI
stand for?You can use the d3-format package modeled after Python Advanced String Formatting PEP3101 :
Give Credit to Waylon Flinn if you like this
This was improved from his more elegant approach to handle negative numbers and ".0" case.
The fewer loops and "if" cases you have, the better IMO.
jsFiddle with test cases -> https://jsfiddle.net/xyug4nvz/7/
this is is quite elegant.
Further improving @Yash's answer with negative number support:
Sounds like this should work for you: