The creation of my iPhone app continues apace, and I’m quite impressed with how much I’ve been learning. It’s taking some time to build the app, but I’m taking everything a step at a time and learning as I go. I don’t just want to throw something together and forget it. I need to LearnNewStuff.
One of today’s many little pieces of new code included a function to turn numbers into words. I’ve no idea if SwiftUI already has a way to do this. Trawling the Internet for answers loses its shine when everything you find is for questions posed 8-12 years ago!
Anyway, to do this little code conversion, I retrieved my old PHP function and converted it into SwiftUI.
PHP:
function number_to_word($number) {
global $setting;
$word = $number;
if($number <= 12) {
$word = $setting['num_array'][$number];
}
if($number > 12 && $number < 20) {
$word = $setting['num_array'][$number] . "teen";
}
if($number >=20 && $number < 100) {
$first_word = $setting['ten_array'][substr($number,0,1)];
$second_word = "";
$second_digit = substr($number,1,1);
if($second_digit != 0) {
$second_word = "-" . $setting['num_array'][substr($number,1,1)];
}
$word = $first_word . $second_word;
}
return $word;
}
SwiftUI:
func number2word(number: Int) -> String {
var word: String = String(number)
let numArray: [String] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thir", "for", "fif", "six", "seven", "eight", "nine"]
let tenArray: [String] = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
if number <= 12 {
word = numArray[number]
}
if number > 12 && number < 20 {
word = numArray[number] + "teen"
}
if number >= 20 && number < 100 {
let tenWord = String(String(number).first!)
let firstWord = tenArray[Int(tenWord) ?? 0]
var secondWord = ""
let secondNum = String(String(number).last!)
if secondNum != "0" && secondNum != "" {
secondWord = "-" + numArray[Int(secondNum) ?? 0]
}
word = firstWord + secondWord
}
return word
}
The main difference between the two pieces of code here is that, in the PHP version, the array is retrieved from data stored in a MariaDB table (I had the site set up so that all variables used in settings for the site were held in a MariaDB table so that I could alter them through the browser instead of changing hard-coded values), but the data in the array is just the same for both pieces of code.
let number = 23
Text(String("\(number2word(number: number))")
// Output will be "twenty-three"
The trickiest thing here was to work out how to get the first and last digits of a two-digit number and then convert to the correct type for what SwiftUI is doing at the time. It seems to work, though.
Feeling suitably elated with this minor success, I wondered if I could use the same code but in an extension. I’ve recently learned to use an extension to remove trailing zeros from a Double, but this time I would be writing my own extension based on the above script.
Extension
extension Int {
var number2word: String {
var word: String = String(self)
let numArray: [String] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thir", "for", "fif", "six", "seven", "eight", "nine"]
let tenArray: [String] = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
if self <= 12 {
word = numArray[self]
}
if self > 12 && self < 20 {
word = numArray[self] + "teen"
}
if self >= 20 && self < 100 {
let tenWord = String(String(self).first!)
let firstWord = tenArray[Int(tenWord) ?? 0]
var secondWord = ""
let secondNum = String(String(self).last!)
if secondNum != "0" && secondNum != "" {
secondWord = "-" + numArray[Int(secondNum) ?? 0]
}
word = firstWord + secondWord
}
return word
}
}
Now, it’s simply a matter of calling up:
let number = 23
Text(number.number2word)
// Output will be "twenty-three"
I’ll call that a success!