-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
|
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 基於以上結果,小於零時將程式碼改成 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;
}; |
ok |
taking 3 hrs |
改成 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
參考:
The text was updated successfully, but these errors were encountered: