Reputation: 3357
I was wondering if there's a way to improve and make this function faster, less resource consuming and more readable:
calculateMerchantEarnings(original_price) {
const processor_percentage = Math.round((original_price * 0.029 + Number.EPSILON) * 100) / 100;
const processor_flat = 30; // in cents
const myfees_and_processing_fees = processor_percentage + processor_flat + 100;
const final_price_minus_deductions = original_price - myfees_and_processing_fees;
const final_price_in_cents = Math.round((final_price_minus_deductions + Number.EPSILON) * 100) / 100;
return (
Math.round((final_price_in_cents / 100 + Number.EPSILON) * 100) / 100
);
}
What I do is:
I have a feeling I may be able to do all this with one line :)
Upvotes: 1
Views: 41
Reputation: 386550
Basically, you could add all into a single expression.
function calculateMerchantEarnings(original_price) {
const processor_percentage = Math.round((original_price * 0.029 + Number.EPSILON) * 100) / 100;
const processor_flat = 30; // in cents
const myfees_and_processing_fees = processor_percentage + processor_flat + 100;
const final_price_minus_deductions = original_price - myfees_and_processing_fees;
const final_price_in_cents = Math.round((final_price_minus_deductions + Number.EPSILON) * 100) / 100;
return Math.round((final_price_in_cents / 100 + Number.EPSILON) * 100) / 100;
}
const fn = x => Math.round(x * 0.971 - 130) / 100;
console.log(calculateMerchantEarnings(1000));
console.log(fn(1000));
Upvotes: 2