| 2 | module roman; |
| 3 | |
| 4 | std::string to_roman(unsigned int i) |
| 5 | { |
| 6 | if (i > 3999) return {}; // 3999, or MMMCMXCIX, is the largest standard Roman numeral |
| 7 | static const std::string ms[]{ "","M","MM","MMM" }; |
| 8 | static const std::string cds[]{ "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" }; |
| 9 | static const std::string xls[]{ "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" }; |
| 10 | static const std::string ivs[]{ "","I","II","III","IV","V","VI","VII","VIII","IX" }; |
| 11 | return ms[i / 1000] + cds[(i % 1000) / 100] + xls[(i % 100) / 10] + ivs[i % 10]; |
| 12 | } |