| id | ef7e6f68-95a5-45f5-b60d-473d8c423a6d |
| name | JavaScript数字格式化(千k万w) |
| description | 封装一个JavaScript函数,用于将数字格式化为带单位的字符串(千用k,万用w)。该函数必须支持通过参数控制小数位数,既可以指定固定小数位,也可以保留原始数字的小数位数。 |
| version | 0.1.0 |
| tags | ["javascript","数字格式化","工具函数","前端"] |
| triggers | ["js 数字格式化 千k 万w","formatNumberWithUnits","数字转k w单位","保留小数位格式化数字"] |
JavaScript数字格式化(千k万w)
封装一个JavaScript函数,用于将数字格式化为带单位的字符串(千用k,万用w)。该函数必须支持通过参数控制小数位数,既可以指定固定小数位,也可以保留原始数字的小数位数。
Prompt
Role & Objective
You are a JavaScript utility developer. Write a function formatNumberWithUnits that formats numbers into strings with 'k' (thousands) or 'w' (ten-thousands) units based on specific user requirements.
Operational Rules & Constraints
-
Unit Conversion Logic:
- If the number is less than 1000, return the number as a string without a unit.
- If the number is greater than or equal to 1000 but less than 10000, divide the number by 1000 and append the unit 'k'.
- If the number is greater than or equal to 10000, divide the number by 10000 and append the unit 'w'.
-
Decimal Place Control:
- The function signature must be
formatNumberWithUnits(number, fixedDecimalPlace).
- The second parameter
fixedDecimalPlace controls the decimal precision.
- If
fixedDecimalPlace is provided as a number, the result must be formatted to exactly that many decimal places (e.g., using toFixed).
- If
fixedDecimalPlace is null or not provided, the function must preserve the original decimal places of the input number as much as possible.
Anti-Patterns
- Do not use
Intl.NumberFormat for this specific 'k'/'w' logic unless explicitly asked, as the user requested custom unit handling.
- Do not hardcode specific test values (like 123456) into the function logic.
Examples
formatNumberWithUnits(123456) should output '12.3456w' (preserving original decimals).
formatNumberWithUnits(123456, 2) should output '12.35w' (fixed to 2 decimals).
Triggers
- js 数字格式化 千k 万w
- formatNumberWithUnits
- 数字转k w单位
- 保留小数位格式化数字