From 2347e9a9f42784caea19c097fe5615f51574fab9 Mon Sep 17 00:00:00 2001 From: smk762 Date: Sat, 21 Dec 2024 00:47:29 +0800 Subject: [PATCH] fix numeric column formatting --- atomic_defi_design/Dex/Constants/General.qml | 54 +++++++++++++------ .../Trade/SimpleView/SubBestOrder.qml | 2 +- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/atomic_defi_design/Dex/Constants/General.qml b/atomic_defi_design/Dex/Constants/General.qml index 578aa1cd3..ca255b8e9 100644 --- a/atomic_defi_design/Dex/Constants/General.qml +++ b/atomic_defi_design/Dex/Constants/General.qml @@ -621,15 +621,21 @@ QtObject { return (show_prefix ? prefix : '') + parseFloat(value).toFixed(3) + ' %' } + + function formatCexRates(value) { + if (value === "0") return "N/A" + if (parseFloat(value) > 0) { + return "+"+formatNumber(value, 2)+"%" + } + return formatNumber(value, 2)+"%" + } + + readonly property int defaultPrecision: 8 readonly property int sliderDigitLimit: 9 readonly property int recommendedPrecision: -1337 function getDigitCount(v) { - console.log("===========") - console.log(typeof(v)) - console.log(v) - console.log("===========") return v.toString().replace("-", "").split(".")[0].length } @@ -645,18 +651,38 @@ QtObject { * @param {number} extra_decimals - The number of decimal places to include if no suffix (default is 8). * @returns {string} - The formatted string. */ - function formatNumber(num, decimals = 2, extra_decimals = 8) { - if (isNaN(num) || num === null) return "0"; + function formatNumber(num, decimals = 8) { + let r = "0"; + let suffix; - const suffixes = ['', 'K', 'M', 'B', 'T']; // Add more as needed for larger numbers - const tier = Math.floor(Math.log10(Math.abs(num)) / 3); // Determine the tier (e.g., thousands, millions) + if (isNaN(num) || num === null) { + return r; + } - if (tier === 0) return num.toFixed(extra_decimals); // No suffix needed + if (typeof(num) == 'string') { + num = parseFloat(num) + } - const scaled = num / Math.pow(10, tier * 3); - const suffix = suffixes[tier] || `e${tier * 3}`; // Use scientific notation if beyond defined suffixes + const suffixes = ['', 'K', 'M', 'B', 'T']; // Add more as needed for larger numbers + const tier = Math.floor(Math.log10(Math.abs(num)) / 3); // Determine the tier (e.g., thousands, millions) - return `${scaled.toFixed(decimals)}${suffix}`; + if (tier === 0) { + r = num.toFixed(decimals); + return r + } + if (0 <= tier && tier <= suffixes.length) { + suffix = suffixes[tier] + num = (num / Math.pow(10, tier * 3)); + } + else if (tier < 0 && num > 0.00000001 ) { + suffix = '' + } + else { + suffix = "e" + tier * 3 + num = (num / Math.pow(10, tier * 3)); + } + r = num.toFixed(decimals) + "" + suffix; + return r; } function formatDouble(v, sf = defaultPrecision, trail_zeros = true) { @@ -672,10 +698,6 @@ QtObject { } function getComparisonScale(value) { - console.log("-----------") - console.log(typeof(value)) - console.log(value) - console.log("-----------") return Math.min(Math.pow(10, getDigitCount(parseFloat(value))), 1000000000) } diff --git a/atomic_defi_design/Dex/Exchange/Trade/SimpleView/SubBestOrder.qml b/atomic_defi_design/Dex/Exchange/Trade/SimpleView/SubBestOrder.qml index 4004c9c86..5a58a1e34 100644 --- a/atomic_defi_design/Dex/Exchange/Trade/SimpleView/SubBestOrder.qml +++ b/atomic_defi_design/Dex/Exchange/Trade/SimpleView/SubBestOrder.qml @@ -215,7 +215,7 @@ DexListView Layout.preferredWidth: _cexRateColumnSize horizontalAlignment: Text.AlignRight color: cex_rates=== "0" ? Qt.darker(DexTheme.foregroundColor) : parseFloat(cex_rates)>0? DexTheme.warningColor : DexTheme.okColor - text_value: Constants.General.getCexRate(cex_rates) + text_value: Constants.General.formatCexRates(cex_rates) opacity: !_isCoinEnabled? .3 : 1 }