2022-04-10 14:08:30 +00:00
|
|
|
pub fn int_to_roman(number: i32) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
|
|
|
|
for (index, digit) in number.to_string().chars().rev().enumerate() {
|
|
|
|
if digit == '0' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let digit = digit.to_digit(10).unwrap() as usize;
|
|
|
|
let magnitude = 10_usize.pow(index as u32);
|
|
|
|
|
|
|
|
let (unit_1, unit_5, unit_10) = match magnitude {
|
|
|
|
1 => ("I", "V", "X"),
|
|
|
|
10 => ("X", "L", "C"),
|
|
|
|
100 => ("C", "D", "M"),
|
|
|
|
1000 => ("M", "", ""),
|
2022-04-11 12:19:21 +00:00
|
|
|
_ => unreachable!(),
|
2022-04-10 14:08:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
result += &match digit {
|
|
|
|
1..=3 => unit_1.repeat(digit),
|
|
|
|
4 => format!("{unit_5}{unit_1}"),
|
|
|
|
5 => unit_5.to_string(),
|
|
|
|
6..=8 => format!("{}{unit_5}", unit_1.repeat(digit - 5)),
|
|
|
|
9 => format!("{unit_10}{unit_1}"),
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.chars().rev().collect()
|
|
|
|
}
|