111
This commit is contained in:
@@ -97,19 +97,27 @@ class _TradePageState extends State<TradePage>
|
||||
if (_tradeType == 0) {
|
||||
return _availableUsdt;
|
||||
} else {
|
||||
// 賣出:qty * price 截斷到2位
|
||||
final qty = double.tryParse(_availableCoinQty) ?? 0;
|
||||
return (qty * price).toStringAsFixed(2);
|
||||
return ((qty * price * 100).truncateToDouble() / 100).toStringAsFixed(2);
|
||||
}
|
||||
}
|
||||
|
||||
/// 計算數量
|
||||
/// 計算數量(向下截斷到4位小數,確保 price * quantity <= amount)
|
||||
String get _calculatedQuantity {
|
||||
final amount = double.tryParse(_amountController.text) ?? 0;
|
||||
final price = _selectedCoin?.price ?? 0;
|
||||
if (price <= 0 || amount <= 0) return '0';
|
||||
// 向下截斷到4位小數,避免回算超出金額
|
||||
final qty = amount / price;
|
||||
return ((qty * 10000).truncateToDouble() / 10000).toStringAsFixed(4);
|
||||
// 使用與後端一致的截斷邏輯:先算原始數量,截斷到4位,再回算金額確保不超
|
||||
final rawQty = amount / price;
|
||||
final truncatedQty = (rawQty * 10000).truncateToDouble() / 10000;
|
||||
// 回算:roundedPrice * truncatedQty,確保不超過 amount
|
||||
final roundedPrice = (price * 100).truncateToDouble() / 100;
|
||||
if (roundedPrice * truncatedQty > amount) {
|
||||
// 回退一個最小單位(0.0001)
|
||||
return (truncatedQty - 0.0001).toStringAsFixed(4);
|
||||
}
|
||||
return truncatedQty.toStringAsFixed(4);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -208,11 +216,16 @@ class _TradePageState extends State<TradePage>
|
||||
void _fillPercent(double pct) {
|
||||
final max = double.tryParse(_maxAmount) ?? 0;
|
||||
final value = max * pct;
|
||||
// 向下截斷到2位小數,避免四捨五入超出可用餘額
|
||||
// 向下截斷到2位小數,再減0.01作為安全緩衝,避免精度問題導致餘額不足
|
||||
final truncated = ((value * 100).truncateToDouble() / 100);
|
||||
final safe = truncated > 0.01 ? truncated - 0.01 : truncated;
|
||||
_amountController.text = safe.toStringAsFixed(2);
|
||||
|
||||
if (_tradeType == 0) {
|
||||
// 買入:向下截斷到2位小數
|
||||
_amountController.text =
|
||||
((value * 100).truncateToDouble() / 100).toStringAsFixed(2);
|
||||
} else {
|
||||
// 賣出:_maxAmount 已是 qty*price 的四捨五入值,直接截斷
|
||||
_amountController.text =
|
||||
((value * 100).truncateToDouble() / 100).toStringAsFixed(2);
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user