fix bug
This commit is contained in:
@@ -24,3 +24,58 @@ export function useTowDecimal(num: string | number): string | number {
|
||||
export function useFormat(num: string | number, unit = '') {
|
||||
return usePrice(Math.abs(+useTowDecimal(num)), true, unit)
|
||||
}
|
||||
|
||||
type TransactionPriceSource = {
|
||||
cash?: number | null
|
||||
gold?: number | null
|
||||
order?: {
|
||||
price?: number | null
|
||||
total_price?: number | null
|
||||
amount?: number | null
|
||||
} | null
|
||||
}
|
||||
|
||||
export function getTransactionTradePrice(item: TransactionPriceSource): number {
|
||||
const orderPrice = Number(item.order?.price)
|
||||
if (Number.isFinite(orderPrice) && orderPrice > 0) {
|
||||
return Math.round(orderPrice)
|
||||
}
|
||||
|
||||
const cashAmount = Math.abs(Number(item.cash) || 0)
|
||||
const goldAmount =
|
||||
Math.abs(Number(item.gold) || 0) || Math.abs(Number(item.order?.amount) || 0)
|
||||
|
||||
if (goldAmount > 0 && cashAmount > 0) {
|
||||
return Math.round(cashAmount / goldAmount)
|
||||
}
|
||||
|
||||
const totalPrice = Number(item.order?.total_price)
|
||||
if (Number.isFinite(totalPrice) && totalPrice > 0) {
|
||||
return Math.round(totalPrice)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
export function getTransactionGoldAmount(item: TransactionPriceSource): number {
|
||||
const gold = Number(item.gold) || 0
|
||||
if (Math.abs(gold) > 0) {
|
||||
return gold
|
||||
}
|
||||
|
||||
const orderAmount = Number(item.order?.amount) || 0
|
||||
if (orderAmount > 0) {
|
||||
const cash = Number(item.cash) || 0
|
||||
if (cash < 0) return orderAmount
|
||||
if (cash > 0) return -orderAmount
|
||||
return orderAmount
|
||||
}
|
||||
|
||||
return gold
|
||||
}
|
||||
|
||||
export function formatGoldAmount(amount?: number | null): string {
|
||||
const num = Number(amount)
|
||||
if (!Number.isFinite(num)) return '0'
|
||||
return String(Number(num.toFixed(3)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user