AutoSkill JavaScript数字格式化(千k万w)

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

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/javascript数字格式化-千k万w" ~/.claude/skills/ecnu-icalk-autoskill-javascript-k-w-197fed && rm -rf "$T"
manifest: SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/javascript数字格式化-千k万w/SKILL.md
source content

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