Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

roundToDecimalPlaces 更新 #1184

Closed
TzuHanLiang opened this issue Sep 7, 2023 · 5 comments · Fixed by #1187
Closed

roundToDecimalPlaces 更新 #1184

TzuHanLiang opened this issue Sep 7, 2023 · 5 comments · Fixed by #1187
Assignees
Labels
3 hard level 3 bug Something isn't working
Milestone

Comments

@TzuHanLiang
Copy link
Member

參考:

export const roundUp = (number: number, decimal: number, condition = false) => {
  if (SafeMath.eq(number, 0)) return `0.${'0'.repeat(decimal)}`;
  if (condition) {
    if (SafeMath.lt(number, 0)) {
      return `-${
        Math.ceil((Math.abs(number) + Number.EPSILON) * Math.pow(10, decimal)) /
        Math.pow(10, decimal)
      }`;
    } else if (SafeMath.gt(number, 0)) {
      return `${
        Math.floor((number + Number.EPSILON) * Math.pow(10, decimal)) /
        Math.pow(10, decimal)
      }`;
    }
  } else {
    return `${
      Math.ceil((number + Number.EPSILON) * Math.pow(10, decimal)) /
      Math.pow(10, decimal)
    }`;
  }
};
@TzuHanLiang TzuHanLiang added bug Something isn't working 1 hard level 1 labels Sep 7, 2023
@TzuHanLiang TzuHanLiang added this to the v0.8.0 Alpha milestone Sep 7, 2023
@arealclimber arealclimber added 3 hard level 3 and removed 1 hard level 1 labels Sep 7, 2023
@arealclimber
Copy link
Member

arealclimber commented Sep 7, 2023

  • condition=true (正數則捨去兩位小數之後的小數、負數則-0.01)

    • Long Close Price (Sell)
    • Short Open Price (Sell)
    • Long Close Value (Sell)
    • Long Open Value (Buy)
    • Short Open Value (Sell)
    • Short Close Value (Buy)
    • Liquidation Price for Long
    • PnL
    • Long/Short TP/SL bound
    • Expected Long/Short Profit
    • Expected Long/Short Loss
  • condition=false (正數或負數都+0.01)

    • Long Open Price (Buy)
    • Short Close Price (Buy)
    • Liquidation Price for Short
    • Guaranteed stop fee
    • Required Margin

@arealclimber
Copy link
Member

console.log('-3.411 floor', Math.floor((-3.411 + Number.EPSILON) * factor) / factor);
// -3.411 floor -3.42

console.log('-3.411 ceil', Math.ceil((-3.411 + Number.EPSILON) * factor) / factor);
// -3.411 ceil -3.41

console.log('3.411 floor', Math.floor((3.411 + Number.EPSILON) * factor) / factor);
// 3.411 floor 3.41

console.log('3.411 ceil', Math.ceil((3.411 + Number.EPSILON) * factor) / factor);
// 3.411 ceil 3.42

基於以上結果,小於零時將程式碼改成 return (Math.floor((Math.abs(number) + Number.EPSILON) * factor) / factor) * -1;

export const roundToDecimalPlaces = (
  number: number,
  decimal: number,
  condition = false
): number => {
  const factor = Math.pow(10, decimal);

  if (SafeMath.eq(number, 0)) {
    return Number(`0.${'0'.repeat(decimal)}`);
  }

  if (condition) {
    if (SafeMath.lt(number, 0)) {
      return (Math.floor((Math.abs(number) + Number.EPSILON) * factor) / factor) * -1;
    } else if (SafeMath.gt(number, 0)) {
      return Math.floor((number + Number.EPSILON) * factor) / factor;
    }
  }

  return Math.ceil((number + Number.EPSILON) * factor) / factor;
};

@TzuHanLiang
Copy link
Member Author

ok

@arealclimber
Copy link
Member

taking 3 hrs

@arealclimber
Copy link
Member

改成 condition 打開就正數則捨去兩位小數之後的小數、負數則減掉0.01,而非直接無條件捨去

export const roundToDecimalPlaces = (
  number: number,
  decimal: number,
  condition = false
): number => {
  const factor = Math.pow(10, decimal);

  if (SafeMath.eq(number, 0)) {
    return Number(`0.${'0'.repeat(decimal)}`);
  }

  if (condition) {
    if (SafeMath.lt(number, 0)) {
      return (Math.ceil((Math.abs(number) + Number.EPSILON) * factor) / factor) * -1;
    } else if (SafeMath.gt(number, 0)) {
      return Math.floor((number + Number.EPSILON) * factor) / factor;
    }
  }

  return Math.ceil((number + Number.EPSILON) * factor) / factor;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 hard level 3 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants