Files
boursecu_admin/src/utils/productCashFee.ts
T

50 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** پارس مقدار هزینه خرید/فروش (نقدی یا درصدی) قبل از ارسال به API */
function normalizeDecimalString(value: string) {
const trimmed = value.trim()
if (!trimmed.includes(',') || trimmed.includes('.')) {
return trimmed.replaceAll(',', '')
}
// 0,5 => 0.5
return trimmed.replace(',', '.')
}
export function parseCashFeeValue(value: unknown): number {
if (value === undefined || value === null) {
return 0
}
const raw = normalizeDecimalString(String(value))
if (raw === '') {
return 0
}
const num = Number(raw)
return Number.isFinite(num) ? num : 0
}
export function normalizeCashFeeFlags(value: unknown): boolean {
if (value === true || value === 1 || value === '1' || value === 'true') {
return true
}
return false
}
export function formatAdminCashFeeLabel(
value: number | null | undefined,
isPercent?: boolean | null,
) {
const raw = Number(value ?? 0)
if (!raw) {
return '۰'
}
if (isPercent) {
const text = Number.isInteger(raw) ? String(raw) : String(parseFloat(raw.toFixed(4)))
return `${text}%`
}
return String(raw)
}