Problems › TypeScript › finance
Convert between currencies
A money-changer converts an amount in minor units at a rate quoted in basis points (per ten thousand).
- The result is floor(amount × rate / 10000), staying in minor units.
- Basis points are tenths of a percent: 10000 bps means the rate is exactly 1.
- A zero amount or zero rate converts to zero.
currencyConvert(amountMinor: int, rateBps: int) → int
Where you start
function currencyConvert(amountMinor: number, rateBps: number): number {
}
Worked examples
| Call | Result |
|---|---|
currencyConvert(1000, 8000) | 800 |
currencyConvert(500, 10000) | 500 |
currencyConvert(1250, 3333) | 416 |
currencyConvert(0, 5000) | 0 |
Hint
Multiply then divide by 10000, rounding down.
Reference solution in TypeScript
function currencyConvert(amountMinor: number, rateBps: number): number {
return Math.floor((amountMinor * rateBps) / 10000);
}