Skip to content

Commit

Permalink
fix: rounding always rounds up
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Aug 28, 2024
1 parent 8b49e0d commit d199986
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/web/components/place-limit-tool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,37 @@ export interface PlaceLimitToolProps {
onOrderSuccess?: (baseDenom?: string, quoteDenom?: string) => void;
}

/* Roundes a given number to the given precision
* i.e. roundUpToDecimal(0.23456, 2) = 0.24
*/
function roundUpToDecimal(value: number, precision: number) {
const multiplier = Math.pow(10, precision || 0);
return Math.ceil(value * multiplier) / multiplier;
}

/**
* Fixes a given string representation of a number to the given decimal count
* Rounds to the decimal count if rounding is true
*/
const fixDecimalCount = (
value: string,
decimalCount = 18,
rounding = false
) => {
if (rounding) return parseFloat(value).toFixed(decimalCount);
if (rounding) {
return roundUpToDecimal(parseFloat(value), decimalCount).toString();
}
const split = value.split(".");
const result =
split[0] +
(decimalCount > 0 ? "." + split[1].substring(0, decimalCount) : "");
return result;
};

/**
* Transforms a given amount to the given decimal count and handles period inputs
* Rounds to the decimal count if rounding is true
*/
const transformAmount = (
value: string,
decimalCount = 18,
Expand Down

0 comments on commit d199986

Please sign in to comment.