system-praktyk-front/src/utils/numbers.ts
2020-08-10 20:13:28 +02:00

33 lines
607 B
TypeScript

const roman = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
};
type RomanLiteral = keyof typeof roman;
// shamefully stolen from https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript
export function convertToRoman(number: number) {
let result = '';
for (const i in roman) {
const q = Math.floor(number / roman[i as RomanLiteral]);
number -= q * roman[i as RomanLiteral];
result += i.repeat(q);
}
return result;
}