Skip to content

Commit

Permalink
added toRomanNumeral() utility
Browse files Browse the repository at this point in the history
  • Loading branch information
vytdev committed Feb 23, 2024
1 parent b1fd5a7 commit c124a15
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/catalyst/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,38 @@ export function formatNumber(n: number): string {
// thx: https://stackoverflow.com/questions/2901102
return n.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}

/**
* convert number into roman numeral
* @param n the number
* @returns result string
*/
export function toRomanNumeral(num: number) {

Check failure on line 83 in src/catalyst/core/utils.ts

View workflow job for this annotation

GitHub Actions / build

'toRomanNumeral' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
// NOTES:
// - no negative
// - no decimals
// - no zeros

// num is higher than the representable roman number
if (num > 3999) return num.toString();

// in minecraft, unicode diacritic `\u0305` (macron) may not display
// correctly, better to not add them

// i used recursion because its easy to debug :)
if(num >= 1000) return 'M' + toRomanNumeral(num - 1000);
if(num >= 900) return 'CM' + toRomanNumeral(num - 900);
if(num >= 500) return 'D' + toRomanNumeral(num - 500);
if(num >= 400) return 'CD' + toRomanNumeral(num - 400);
if(num >= 100) return 'C' + toRomanNumeral(num - 100);
if(num >= 90) return 'XC' + toRomanNumeral(num - 90);
if(num >= 50) return 'L' + toRomanNumeral(num - 50);
if(num >= 40) return 'XL' + toRomanNumeral(num - 40);
if(num >= 10) return 'X' + toRomanNumeral(num - 10);
if(num >= 9) return 'IX' + toRomanNumeral(num - 9);
if(num >= 5) return 'V' + toRomanNumeral(num - 5);
if(num >= 4) return 'IV' + toRomanNumeral(num - 4);
if(num >= 1) return 'I' + toRomanNumeral(num - 1);

return '';
}

0 comments on commit c124a15

Please sign in to comment.