Skip to content

Commit

Permalink
bitshares#3525 Fix exponential numbers on Exchange & MarketDepth
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorml committed Nov 23, 2022
1 parent 67ecb13 commit 4e2a526
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
7 changes: 4 additions & 3 deletions app/components/Exchange/DepthHighChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Translate from "react-translate-component";
import colors from "assets/colors";
import AssetName from "../Utility/AssetName";
import {didOrdersChange} from "common/MarketClasses";
import {numberExponentToLarge} from "../../lib/common/numberExplonentConversion";

class DepthHighChart extends React.Component {
shouldComponentUpdate(nextProps) {
Expand Down Expand Up @@ -192,10 +193,10 @@ class DepthHighChart extends React.Component {
labels: {
style: {
color: primaryText
},
formatter: function() {
return numberExponentToLarge(this.value);
}
// formatter: function() {
// return this.value / power;
// }
},
ordinal: false,
lineColor: "#000000",
Expand Down
13 changes: 7 additions & 6 deletions app/components/Exchange/Exchange.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import SimpleDepositBlocktradesBridge from "../Dashboard/SimpleDepositBlocktrade
import {Notification} from "bitshares-ui-style-guide";
import PriceAlert from "./PriceAlert";
import counterpart from "counterpart";
import {numberExponentToLarge} from "../../lib/common/numberExplonentConversion";

class Exchange extends React.Component {
static propTypes = {
Expand Down Expand Up @@ -2154,9 +2155,9 @@ class Exchange extends React.Component {
this,
"bid"
)}
amount={bid.toReceiveText}
price={bid.priceText}
total={bid.forSaleText}
amount={numberExponentToLarge(bid.toReceiveText)}
price={numberExponentToLarge(bid.priceText)}
total={numberExponentToLarge(bid.forSaleText)}
quote={quote}
base={base}
amountChange={this._onInputReceive.bind(
Expand Down Expand Up @@ -2311,9 +2312,9 @@ class Exchange extends React.Component {
}}
type="ask"
hideHeader={true}
amount={ask.forSaleText}
price={ask.priceText}
total={ask.toReceiveText}
amount={numberExponentToLarge(ask.forSaleText)}
price={numberExponentToLarge(ask.priceText)}
total={numberExponentToLarge(ask.toReceiveText)}
quote={quote}
base={base}
expirationType={expirationType["ask"]}
Expand Down
29 changes: 29 additions & 0 deletions app/lib/common/numberExplonentConversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// taken from: https://stackoverflow.com/a/62124842/3930054

export function numberExponentToLarge(numIn) {
numIn += ""; // To cater to numric entries
let sign = ""; // To remember the number sign
numIn.charAt(0) == "-" && (numIn = numIn.substring(1), sign = "-"); // remove - sign & remember it
let str = numIn.split(/[eE]/g); // Split numberic string at e or E
if (str.length < 2) return sign + numIn; // Not an Exponent Number? Exit with orginal Num back
let power = str[1]; // Get Exponent (Power) (could be + or -)

let deciSp = 1.1.toLocaleString().substring(1, 2); // Get Deciaml Separator
str = str[0].split(deciSp); // Split the Base Number into LH and RH at the decimal point
let baseRH = str[1] || "", // RH Base part. Make sure we have a RH fraction else ""
baseLH = str[0]; // LH base part.

if (power >= 0) { // ------- Positive Exponents (Process the RH Base Part)
if (power > baseRH.length) baseRH += "0".repeat(power - baseRH.length); // Pad with "0" at RH
baseRH = baseRH.slice(0, power) + deciSp + baseRH.slice(power); // Insert decSep at the correct place into RH base
if (baseRH.charAt(baseRH.length - 1) == deciSp) baseRH = baseRH.slice(0, -1); // If decSep at RH end? => remove it

} else { // ------- Negative exponents (Process the LH Base Part)
let num = Math.abs(power) - baseLH.length; // Delta necessary 0's
if (num > 0) baseLH = "0".repeat(num) + baseLH; // Pad with "0" at LH
baseLH = baseLH.slice(0, power) + deciSp + baseLH.slice(power); // Insert "." at the correct place into LH base
if (baseLH.charAt(0) == deciSp) baseLH = "0" + baseLH; // If decSep at LH most? => add "0"

}
return sign + (baseLH + baseRH).replace(/^0*(\d+|\d+\.\d+?)\.?0*$/, "$1");
}

0 comments on commit 4e2a526

Please sign in to comment.