1234567891011121314151617181920212223242526272829303132333435363738 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Document</title>
- </head>
- <body>
- <script>
- const formatBigNumber = (number, decimalPlaces = 2) => {
- debugger
- if (typeof number !== 'number' && typeof number !== 'string') {
- return console.warn(`Input must be a number or a string representation of a number. but get ${typeof number}`)
- }
- number = parseFloat(number)
- if (Math.abs(number) < 10000) {
- return { count: number.toFixed(decimalPlaces), unit: '' }
- }
- const units = ['', '万', '亿']
- const unitIndex = Math.floor((parseInt(number).toString().length - 1) / 4)
- const unit = units[Math.min(unitIndex, 2)]
- let result
- if (unitIndex > 0) {
- result = (number / Math.pow(10, unitIndex * 4)).toFixed(decimalPlaces)
- } else {
- result = number.toFixed(decimalPlaces)
- }
- return {
- count: result,
- unit
- }
- }
- formatBigNumber(3323805.27)
- </script>
- </body>
- </html>
|