Created
August 21, 2020 09:17
-
-
Save JSoon/44ae2b420428710c14efe31cf3054c70 to your computer and use it in GitHub Desktop.
格式化数字, 保留小数位精度
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import BigNumber from 'bignumber.js'; | |
| /** | |
| * 格式化数字, 保留小数位精度 | |
| * @param {number} originNumber 数字 | |
| * @param {number} precision 小数位精度 | |
| */ | |
| const formatNumber = (originNumber = 0, precision = 2) => { | |
| if (Number.isNaN(Number(originNumber))) { | |
| throw new Error('数字格式错误'); | |
| } | |
| const number = new BigNumber(Number(originNumber)); | |
| let numberString = number.toFixed(); | |
| // 保留两位小数 | |
| const splitNumberString = numberString.split('.'); | |
| // 如果没有小数位, 则直接补齐精度位数的0 | |
| if (splitNumberString.length === 1) { | |
| numberString = `${numberString}${precision === 0 ? '' : '.'}${'0'.repeat(precision)}`; | |
| } | |
| // 如果有小数位, 则补齐缺少的精度位数 | |
| else { | |
| const integer = splitNumberString[0]; | |
| let decimal = splitNumberString[1]; | |
| if (decimal.length > precision) { | |
| decimal = decimal.slice(0, precision); | |
| } | |
| else { | |
| decimal = `${decimal}${'0'.repeat(precision - decimal.length)}`; | |
| } | |
| numberString = `${integer}${precision === 0 ? '' : '.'}${decimal}`; | |
| } | |
| return numberString; | |
| }; | |
| export { | |
| formatNumber, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment