| "+""+(item.is_alias===true?(""+item.alias+" - see "):"")+item.displayPath+""+name+" | "+""+""+item.desc+" |
"+code.outerHTML+" |
fn:) to \
+ restrict the search to a given item kind.","Accepted kinds are: fn, mod, struct, \
+ enum, trait, type, macro, \
+ and const.","Search functions by type signature (e.g., vec -> usize or \
+ * -> vec)","Search multiple things at once by splitting your query with comma (e.g., \
+ str,u8 or String,struct:Vec,test)","You can look for items with an exact name by putting double quotes around \
+ your request: \"string\"","Look for items inside another one by searching for a path: vec::Vec",].map(function(x){return""+x+"
"}).join("");var div_infos=document.createElement("div");addClass(div_infos,"infos");div_infos.innerHTML="pub fn digit_to_text(digit: u8) -> Option<String>
Convert digit to text.
+pub fn to_text(number: u32) -> String
Convert u32 to words in a string.
+| digit_to_text | Convert digit to text. + |
| to_text | Convert u32 to words in a string. + |
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 + 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 + 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 + 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +
+mod test; +/// Convert digit to text. +pub fn digit_to_text(digit: u8) -> Option<String> { + match digit { + 0 => Some("".to_string()), + 1 => Some("one".to_string()), + 2 => Some("two".to_string()), + 3 => Some("three".to_string()), + 4 => Some("four".to_string()), + 5 => Some("five".to_string()), + 6 => Some("six".to_string()), + 7 => Some("seven".to_string()), + 8 => Some("eight".to_string()), + 9 => Some("nine".to_string()), + _ => None, + } +} + +fn tens_place(tens: u8, ones: u8) -> Option<String> { + match tens { + 0 => digit_to_text(ones), + 1 => match ones { + 0 => Some("ten".to_string()), + 1 => Some("eleven".to_string()), + 2 => Some("twelve".to_string()), + 3 => Some("thirteen".to_string()), + 4 => Some("fourteen".to_string()), + 5 => Some("fifteen".to_string()), + 6 => Some("sixteen".to_string()), + 7 => Some("seventeen".to_string()), + 8 => Some("eighteen".to_string()), + 9 => Some("nineteen".to_string()), + _ => None, + }, + d @ 2..=5 | d @ 8 => Some({ + let mut buffer = String::new(); + match d { + 2 => buffer.push_str("twenty"), + 3 => buffer.push_str("thirty"), + 4 => buffer.push_str("forty"), + 5 => buffer.push_str("fifty"), + 8 => buffer.push_str("eighty"), + _ => (), + } + buffer.push_str(digit_to_text(ones).unwrap().as_str()); + buffer + }), + d @ 6..=9 => Some({ + let mut string = digit_to_text(d).unwrap() + "ty"; + string.push_str(digit_to_text(ones).unwrap().as_str()); + string + }), + _ => None, + } +} +fn place_value(number: u8, place: u8) -> Option<String> { + 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, + } +} +/// Convert u32 to words in a string. +pub fn to_text(number: u32) -> String { + let mut numtext: String = String::new(); + 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(), + ) + } + } + } + numtext +} +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +
+#[cfg(test)] +mod tests { + #[test] + 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() { + use crate::to_text; + 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() { + use crate::tens_place; + assert_eq!(tens_place(1, 2).unwrap(), "twelve"); + assert_eq!(tens_place(7, 6).unwrap(), "seventysix"); + assert_eq!(tens_place(5, 7).unwrap(), "fiftyseven"); + assert_eq!(tens_place(2, 3).unwrap(), "twentythree"); + assert_eq!(tens_place(6, 9).unwrap(), "sixtynine"); + assert_eq!(tens_place(0, 9).unwrap(), "nine"); + } +} +