Function ntext::digit_to_text [−][src]
pub fn digit_to_text(digit: u8) -> Option<String>
Function ntext::digit_to_text [−][src]
pub fn digit_to_text(digit: u8) -> Option<String>
Convert digit to text.
Function ntext::to_text [−][src]
pub fn to_text(number: u32) -> String
Function ntext::to_text [−][src]
pub fn to_text(number: u32) -> String
Convert u32 to words in a string.
Crate ntext[−][src]
Functions
+Crate ntext[−][src]
+
+
+
+extern crate ntext; +use ntext::digit_to_text; +fn main() { + println!("{}",digit_to_text(1).unwrap()); + assert_eq!("two",digit_to_text(2).unwrap()); + assert_ne!("five",digit_to_text(8).unwrap()); +}
However giving the program a zero will return an empty string.
+ ++extern crate ntext; +use ntext::to_text; +fn main() { + println!("{}",to_text(1312)); + assert_eq!(to_text(1312),"onethousandthreehundredtwelve"); +}
This fucntion will also return an empty string on input zero
+Functions
| digit_to_text | Convert digit to text. |
| to_text | Convert u32 to words in a string. |
+//!```rust +//!extern crate ntext; +//!use ntext::digit_to_text; +//!fn main() { +//! println!("{}",digit_to_text(1).unwrap()); +//! assert_eq!("two",digit_to_text(2).unwrap()); +//! assert_ne!("five",digit_to_text(8).unwrap()); +//!} +//!``` +//! However giving the program a zero will return an empty string. +//! +//!```rust +//!extern crate ntext; +//!use ntext::to_text; +//!fn main() { +//! println!("{}",to_text(1312)); +//! assert_eq!(to_text(1312),"onethousandthreehundredtwelve"); +//!} +//!``` +//! This fucntion will also return an empty string on input zero + mod test; /// Convert digit to text. pub fn digit_to_text(digit: u8) -> Option<String> { @@ -167,56 +202,49 @@ } } fn place_value(number: u8, place: u8) -> Option<String> { + let mut value = digit_to_text(number).unwrap(); match place { - p @ 3..=6 => Some({ - let mut value = digit_to_text(number).unwrap(); - match p { - 3 => value.push_str("hundred"), - 4 => value.push_str("thousand"), - // 5 => value.push_str("placeholder"), - 6 => value.push_str("million"), - _ => (), - } - value - }), - _ => None, - } + 1 => (), + 2 => (), //This should never happen as 2 is included in the tens_place_holders + 3 => value.push_str("hundred"), + 4 => value.push_str("thousand"), + 5 => (), // Souldn't happen + 6 => value.push_str("million"), + _ => (), + }; + Some(value) } + /// Convert u32 to words in a string. pub fn to_text(number: u32) -> String { let mut numtext: String = String::new(); + let mut last: Option<u8> = None; + let tens_place_holders: [u8; 2] = [2, 5]; let digits: Vec<u8> = number .to_string() .chars() .map(|d| d.to_digit(10).unwrap() as u8) .collect(); - let second_digit_array: [usize; 2] = [0, 3]; - let mut last: Option<u8> = None; - 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(), - ) - } + + let mut place: u8 = digits.len() as u8; + + for (_index, digit) in digits.iter().enumerate() { + if tens_place_holders.contains(&place) { + last = Some(*digit); + place -= 1; + continue; } + if let Some(last_digit) = last { + println!("hello index {}", place); + numtext.push_str(tens_place(last_digit, *digit).unwrap().as_str()); + if place > 2 { + numtext.push_str(place_value(0, place).unwrap().as_str()); + } + last = None; + } else if *digit != 0 { + numtext.push_str(place_value(*digit, place).unwrap().as_str()) + } + place -= 1; } numtext } diff --git a/docs/src/ntext/test.rs.html b/docs/src/ntext/test.rs.html index 4163c7d..1b86a22 100644 --- a/docs/src/ntext/test.rs.html +++ b/docs/src/ntext/test.rs.html @@ -30,6 +30,10 @@ 27 28 29 +30 +31 +32 +33
#[cfg(test)] mod tests { @@ -44,10 +48,14 @@ #[test] fn numbers() { use crate::to_text; + + assert_eq!(to_text(1), "one"); + assert_eq!(to_text(10), "ten"); + assert_eq!(to_text(100), "onehundred"); + assert_eq!(to_text(1000), "onethousand"); 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() { diff --git a/src/lib.rs b/src/lib.rs index c3b3d64..1f7e875 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,24 @@ +//!```rust +//!extern crate ntext; +//!use ntext::digit_to_text; +//!fn main() { +//! println!("{}",digit_to_text(1).unwrap()); +//! assert_eq!("two",digit_to_text(2).unwrap()); +//! assert_ne!("five",digit_to_text(8).unwrap()); +//!} +//!``` +//! However giving the program a zero will return an empty string. +//! +//!```rust +//!extern crate ntext; +//!use ntext::to_text; +//!fn main() { +//! println!("{}",to_text(1312)); +//! assert_eq!(to_text(1312),"onethousandthreehundredtwelve"); +//!} +//!``` +//! This fucntion will also return an empty string on input zero + mod test; /// Convert digit to text. pub fn digit_to_text(digit: u8) -> Option{ @@ -54,56 +75,49 @@ fn tens_place(tens: u8, ones: u8) -> Option { } } fn place_value(number: u8, place: u8) -> Option { + let mut value = digit_to_text(number).unwrap(); match place { - p @ 3..=6 => Some({ - let mut value = digit_to_text(number).unwrap(); - match p { - 3 => value.push_str("hundred"), - 4 => value.push_str("thousand"), - // 5 => value.push_str("placeholder"), - 6 => value.push_str("million"), - _ => (), - } - value - }), - _ => None, - } + 1 => (), + 2 => (), //This should never happen as 2 is included in the tens_place_holders + 3 => value.push_str("hundred"), + 4 => value.push_str("thousand"), + 5 => (), // Souldn't happen + 6 => value.push_str("million"), + _ => (), + }; + Some(value) } + /// Convert u32 to words in a string. pub fn to_text(number: u32) -> String { let mut numtext: String = String::new(); + let mut last: Option = None; + let tens_place_holders: [u8; 2] = [2, 5]; let digits: Vec = number .to_string() .chars() .map(|d| d.to_digit(10).unwrap() as u8) .collect(); - let second_digit_array: [usize; 2] = [0, 3]; - let mut last: Option = None; - 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(), - ) - } + + let mut place: u8 = digits.len() as u8; + + for (_index, digit) in digits.iter().enumerate() { + if tens_place_holders.contains(&place) { + last = Some(*digit); + place -= 1; + continue; } + if let Some(last_digit) = last { + println!("hello index {}", place); + numtext.push_str(tens_place(last_digit, *digit).unwrap().as_str()); + if place > 2 { + numtext.push_str(place_value(0, place).unwrap().as_str()); + } + last = None; + } else if *digit != 0 { + numtext.push_str(place_value(*digit, place).unwrap().as_str()) + } + place -= 1; } numtext } diff --git a/src/test.rs b/src/test.rs index c056c2d..bdb7e1c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -11,10 +11,14 @@ mod tests { #[test] fn numbers() { use crate::to_text; + + assert_eq!(to_text(1), "one"); + assert_eq!(to_text(10), "ten"); + assert_eq!(to_text(100), "onehundred"); + assert_eq!(to_text(1000), "onethousand"); 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() {