/*
Convert numbers to English words in Rust
----------------------------------------
Supports numbers from 0 to 999,999,999,999,999.
Strategy:
- Break the number into named groups: trillions, billions, millions,
thousands, hundreds.
- Convert each 0–999 group using a helper function.
- Append group names ("trillion", "billion", etc.) when needed.
- Build the final string using a String buffer.
*/
static ONES: [&str; 20] = [
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
];
static TENS_WORDS: [&str; 10] = [
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety",
];
/*
append_word:
Adds a word to the output buffer with proper spacing.
*/
fn append_word(out: &mut String, word: &str) {
if !word.is_empty() {
if !out.is_empty() {
out.push(' ');
}
out.push_str(word);
}
}
/*
convert_hundreds:
Converts numbers from 0 to 999 into words.
*/
fn convert_hundreds(n: i64, out: &mut String) {
let mut n = n;
if n == 0 {
return;
}
if n >= 100 {
append_word(out, ONES[(n / 100) as usize]);
append_word(out, "hundred");
n %= 100;
if n > 0 {
append_word(out, "and");
}
}
if n >= 20 {
append_word(out, TENS_WORDS[(n / 10) as usize]);
if n % 10 != 0 {
append_word(out, ONES[(n % 10) as usize]);
}
} else if n > 0 {
append_word(out, ONES[n as usize]);
}
}
/*
number_to_words:
Converts any number from 0 to 999,999,999,999,999 into English words.
*/
fn number_to_words(num: i64) -> String {
if num == 0 {
return "zero".to_string();
}
let mut out = String::new();
let trillions: i64 = num / 1_000_000_000_000;
let billions: i64 = (num / 1_000_000_000) % 1000;
let millions: i64 = (num / 1_000_000) % 1000;
let thousands: i64 = (num / 1_000) % 1000;
let hundreds: i64 = num % 1000;
if trillions > 0 {
convert_hundreds(trillions, &mut out);
append_word(&mut out, "trillion");
}
if billions > 0 {
convert_hundreds(billions, &mut out);
append_word(&mut out, "billion");
}
if millions > 0 {
convert_hundreds(millions, &mut out);
append_word(&mut out, "million");
}
if thousands > 0 {
convert_hundreds(thousands, &mut out);
append_word(&mut out, "thousand");
}
if hundreds > 0 {
convert_hundreds(hundreds, &mut out);
}
out
}
/*
main:
Demonstrates usage with multiple test numbers.
*/
fn main() {
let tests: [i64; 8] = [
1_234,
12_345,
123_456,
1_234_567,
123_456_789,
12_345_678_901,
1_234_567_890_123,
999_999_999_999_999,
];
for n in tests {
println!("{} -> {}", n, number_to_words(n));
}
}
/*
run:
1234 -> one thousand two hundred and thirty four
12345 -> twelve thousand three hundred and forty five
123456 -> one hundred and twenty three thousand four hundred and fifty six
1234567 -> one million two hundred and thirty four thousand five hundred and sixty seven
123456789 -> one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine
12345678901 -> twelve billion three hundred and forty five million six hundred and seventy eight thousand nine hundred and one
1234567890123 -> one trillion two hundred and thirty four billion five hundred and sixty seven million eight hundred and ninety thousand one hundred and twenty three
999999999999999 -> nine hundred and ninety nine trillion nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine
*/