preface
Recently just do the project, there is a number too large directly show ten million, billion and other units of the thumbnail display. Get online to check I actually picked up the log operation of my high school mathematics 🤣, good magic! Sure enough, the bottom of the code is mathematics, learn math and physics throughout the world, this sentence is not unreasonable 😬
Implementation approach
Implementation ideas we should be very clear, there are no more than two cases, one is but [ten thousand] when the normal display and another after [ten thousand] thumbnail display
Code implementation
Before we get to that, let’s review a few points
Math.log() takes the logarithm of the number usually math.log (y)/ math.log (x) takes the logarithm of y base x math.pow (x,y) takes the power of y base x toFiexd(x) Not an integer number, rounded to x decimal placesCopy the code
For those of you who have forgotten the log operation as I did here, I am the log operation link, and I attach the link to review 🤞
There are two cases:
2. If the number exceeds 10,000, I determines whether the unit is 10,000, billion or trillion, and value determines whether the number has a decimal part. If it has a decimal part, round it to the decimal part. If there's no decimal part, it's zero zeroCopy the code
Of course, here, I also thought of using a finer division, 100,000 million, etc., that tends to a finer interval, that is, to judge the [1,2] interval decimal problem, the details will be more complicated, if you have written friends welcome to share and discuss 🤘
Ali interview questions
Yesterday I saw an interview question from Ali, which belongs to the same category as this:
I share it directly here
Var num = 123456789 // Format thousandth output num.tolocaleString () // Format thousandth output with $sign Num. ToLocaleString (" en-us ",{style:"currency",currency:"USD"} num.toLocaleString("zh-Hans-CN",{style:"currency",currency:"CNY"})Copy the code
The end of the
There are front-end or more direction of the research masters, like to discuss can add wechat 15754980373🤞, welcome!