如何讓 AI 智能體沿用現有貨幣格式化器,而非自行格式化金額
Give the agent the existing currency formatter, not a one-off toLocaleString for money
Sherotree 建議要求 AI 智能體沿用專案既有貨幣格式化器,避免在功能元件中另用 toLocaleString 或自行拼接貨幣符號與小數位。提示詞應要求沿用共用 helper 的地區設定、貨幣及捨入規則,並附上一處既有用法;審查程式差異時,可搜尋 style: "currency"、JSX 中硬編碼的 $ 或 USD,以及金額上的 toFixed(2)。
The PR shows a price with amount.toLocaleString("en-US", { style: "currency", currency: "USD" }) in the component, or it concatenates a $ and a toFixed(2). The repo already has a formatCurrency (or formatMoney) helper with the product locale, the currency code, and the rounding rules already decided. Nearby invoice and plan rows use it. The one-off call hardcodes a locale the rest of the app does not use, and toFixed does not follow the same rounding or symbol placement.
I've started treating a one-off money string as a policy leak, not as a quicker format. Displaying a number is the easy part. Locale, currency, and consistent rounding are why the helper is there.
Why the one-off formatter shows up
A tutorial "format a price" example is toLocaleString with en-US and USD, or a template literal with a dollar sign. The model can write that without finding lib/formatCurrency or utils/money. Your helper may wrap Intl.NumberFormat, or it may be named formatPrice, which is easy to miss if the prompt only says "show the amount."
You also skip zero-decimal currencies, negative amounts, and the rule about not inventing a second date or number formatter while you are already looking at toLocaleString.
A simple currency policy
- New money display uses the existing currency formatter
- Do not call toLocaleString for currency in a feature component
- Do not prefix a symbol and toFixed in JSX
- A new currency, locale, or rounding rule is a helper change, with a test
If the value is a date or a plain count, that is the existing date or number formatter, not a currency call. If the amount is for input, format on display and keep the stored value numeric.
What to put in the prompt
Use the existing currency formatter for this amount.
Do not call toLocaleString or prefix a currency symbol in this component.
Keep locale, currency, and rounding on the shared helper.
If the value is a date or a plain number, use the existing helpers for those instead.
Paste one existing formatCurrency usage. In review, search the diff for style: "currency", a hardcoded $ or USD in JSX, and toFixed on a money value.
Reject / send back
Reject
- A feature-level price that reimplements currency formatting
- A hardcoded locale or currency that does not match the product helper
- A
$plustoFixed(2)string in the component - A second i18n or money library added for one amount
Accept
- A price that uses the existing currency formatter
- A helper PR that adds a documented currency or rounding case and a test
- A numeric input that stays numeric and formats only on display
來源:DEV Community · dev.to