# JavaScript数字格式化（千k万w）

> 封装一个JavaScript函数，用于将数字格式化为带单位的字符串（千用k，万用w）。该函数必须支持通过参数控制小数位数，既可以指定固定小数位，也可以保留原始数字的小数位数。

- Skill: `ecnu-icalk/javascript-k-w` (Agent Skill)
- Install (CLI): `npx skillmds@latest add ecnu-icalk/javascript-k-w`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ecnu-icalk/javascript-k-w/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: ECNU-ICALK (https://skillmd.com/u/ecnu-icalk)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/ecnu-icalk/javascript-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
1. **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'.

2. **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单位
- 保留小数位格式化数字

