fix(trade): keep exact grams when the input shows rounded display

Percent and cash conversions no longer lose precision by rounding the gram field to three decimals.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-08 14:56:14 +03:30
co-authored by Cursor
parent e9abc9840a
commit 5015fb2838
2 changed files with 69 additions and 15 deletions
+38
View File
@@ -65,6 +65,44 @@ export function formatTradeGramInputValue(grams?: number | null): string | undef
return value.toFixed(TRADE_GRAM_INPUT_DECIMALS)
}
/** متن اینپوت گرم — فقط نمایش سه‌رقمی؛ منبع حقیقت همان گرم دقیق است */
export function tradeGramInputDisplay(exactGrams?: number | string | null): string {
if (exactGrams === undefined || exactGrams === null || exactGrams === '') return ''
const n = Number(exactGrams)
if (!Number.isFinite(n) || n <= 0) return ''
return formatTradeGramInputValue(n) ?? ''
}
/** گرم دقیق از درصد کیف — بدون رند نمایشی اینپوت */
export function exactGoldFromWalletPercent(balance: number, percent: number): number {
if (!(Number(percent) > 0) || !(Number(balance) > 0)) return 0
return normalizeOrderGoldAmount((Number(balance) * Number(percent)) / 100)
}
/**
* اگر متن اینپوت همان نمایش سه‌رقمی مقدار دقیق فعلی باشد، دقیق را نگه می‌دارد.
* در غیر این صورت عدد تایپ‌شده منبع حقیقت می‌شود.
*/
export function goldFromGramInputDisplay(
raw: unknown,
exactGrams?: number | string | null
): number | undefined {
if (raw === '' || raw == null) return undefined
const text = String(raw).trim()
if (text === '') return undefined
const typed = Number(text)
if (!Number.isFinite(typed) || typed <= 0) return undefined
const exact = Number(exactGrams)
if (Number.isFinite(exact) && exact > 0) {
const shown = formatTradeGramInputValue(exact)
if (shown != null && text === shown) {
return normalizeOrderGoldAmount(exact)
}
}
return normalizeOrderGoldAmount(typed)
}
function feeRate() {
return Number(serviceFeePercentage.value)
}