Problems › TypeScript › finance
Profit margin in permille
A store wants its margin as a whole per-mille number: how much of each minor unit of revenue survives as profit.
- Margin is (revenue - cost) × 1000 ÷ revenue, rounded down.
- A revenue of zero or less has no defined margin: return 0.
- A loss yields a negative margin.
profitMargin(revenue: int, cost: int) → int
Where you start
function profitMargin(revenue: number, cost: number): number {
}
Worked examples
| Call | Result |
|---|---|
profitMargin(1000, 600) | 400 |
profitMargin(1000, 1000) | 0 |
profitMargin(1000, 1200) | -200 |
profitMargin(0, 100) | 0 |
Hint
Subtract the cost from revenue, scale by 1000, divide by revenue.
Reference solution in TypeScript
function profitMargin(revenue: number, cost: number): number {
if (revenue <= 0) return 0;
return Math.floor(((revenue - cost) * 1000) / revenue);
}