Fixed to_text not returning single digits
This commit is contained in:
+23
-19
@@ -79,26 +79,30 @@ pub fn to_text(number: u32) -> String {
|
||||
.collect();
|
||||
let second_digit_array: [usize; 2] = [0, 3];
|
||||
let mut last: Option<u8> = None;
|
||||
for (index, digit) in digits.iter().rev().enumerate() {
|
||||
if second_digit_array.contains(&index) {
|
||||
last = Some(digit.clone());
|
||||
continue;
|
||||
}
|
||||
if let Some(last_digit) = last {
|
||||
let mut buffer = String::new();
|
||||
buffer.push_str(tens_place(digit.clone(), last_digit).unwrap().as_str());
|
||||
if index > 2 {
|
||||
buffer.push_str(place_value(0, index as u8).unwrap().as_str());
|
||||
if digits.len() == 1 {
|
||||
numtext.push_str(digit_to_text(*digits.last().unwrap()).unwrap().as_str());
|
||||
} else {
|
||||
for (index, digit) in digits.iter().rev().enumerate() {
|
||||
if second_digit_array.contains(&index) {
|
||||
last = Some(digit.clone());
|
||||
continue;
|
||||
}
|
||||
if let Some(last_digit) = last {
|
||||
let mut buffer = String::new();
|
||||
buffer.push_str(tens_place(digit.clone(), last_digit).unwrap().as_str());
|
||||
if index > 2 {
|
||||
buffer.push_str(place_value(0, index as u8).unwrap().as_str());
|
||||
}
|
||||
numtext.insert_str(0, buffer.as_str());
|
||||
last = None;
|
||||
} else {
|
||||
numtext.insert_str(
|
||||
0,
|
||||
place_value(digit.clone(), index as u8 + 1)
|
||||
.unwrap()
|
||||
.as_str(),
|
||||
)
|
||||
}
|
||||
numtext.insert_str(0, buffer.as_str());
|
||||
last = None;
|
||||
} else {
|
||||
numtext.insert_str(
|
||||
0,
|
||||
place_value(digit.clone(), index as u8 + 1)
|
||||
.unwrap()
|
||||
.as_str(),
|
||||
)
|
||||
}
|
||||
}
|
||||
numtext
|
||||
|
||||
@@ -4,6 +4,9 @@ mod tests {
|
||||
fn digits() {
|
||||
use crate::digit_to_text;
|
||||
assert_eq!(digit_to_text(9).unwrap(), "nine");
|
||||
assert_eq!(digit_to_text(3).unwrap(), "three");
|
||||
assert_eq!(digit_to_text(7).unwrap(), "seven");
|
||||
assert_eq!(digit_to_text(5).unwrap(), "five");
|
||||
}
|
||||
#[test]
|
||||
fn numbers() {
|
||||
@@ -11,6 +14,7 @@ mod tests {
|
||||
assert_eq!(to_text(12345), "twelvethousandthreehundredfortyfive");
|
||||
assert_eq!(to_text(81123), "eightyonethousandonehundredtwentythree");
|
||||
assert_eq!(to_text(12), "twelve");
|
||||
assert_eq!(to_text(2), "two");
|
||||
}
|
||||
#[test]
|
||||
fn tens() {
|
||||
|
||||
Reference in New Issue
Block a user