Last active
March 24, 2022 08:59
-
-
Save Houserqu/ee066467fa98e4cb09004eff78658bd6 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
| /** | |
| * 格式化价格 | |
| * 2000099 => 20,000.99 元 | |
| * @param num 单位分 | |
| * @param currencyType | |
| * @returns {string} | |
| */ | |
| module.exports = (num, currencyType = null) => { | |
| let value = Number(num).toFixed(2) | |
| let parts = value.toString().split(".") | |
| parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") | |
| // 单位, 如果传了币种,则显示单位 | |
| if (currencyType !== null) { | |
| return parts.join(".") + ' ' + CURRENCY_UNIT_NAME[currencyType] || '元' | |
| } | |
| return parts.join(".") | |
| } | |
| // 正则实现 | |
| function toThousands(num) { | |
| const numStr = (num || 0).toString(); | |
| return numStr.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment