
Nomen mihi est Jasper Camber Holton. scribe, codice, musicam fac, et sum elit Uglek.com.
Has 335 posts, follows 17 users and is followed by 12 users.
Tandem videri at May 24, 2022 10:44




How to Generate a String from a Number in Python I use the following code to generate a string from a number under 1000. It is using simple arrays and if statements to generate a compound number as a string.
import math
n = ['one','two','three','four','five', 'six', 'seven', 'eight', 'nine', 'ten']
tn = ['eleven','twelve','thir','four','fif','six','seven','eigh','nine']
nn = ['teen','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety','hundred']
def number_to_string(num):
if num < 11:
return n[num-1]
if num < 20:
if num < 13:
return tn[num-11]
return tn[num-11] + 'teen'
if num < 100:
extra = '-'+n[num%10-1]
if num%10 == 0:
extra = ''
return nn[math.floor(num/10)-1]+extra
if num < 1000:
extra = '-'+n[num%10-1]
if num%10 == 0:
extra = ''
return n[math.floor(num/100)-1]+'-hundred-'+nn[math.floor((num%100)/10)-1]+extra
return 'number too large to compute!'
for x in range(1,1000):
print(number_to_string(x))

Baby geese by Greenlake! This time of year the geese are hatched and are running around in fluffy formation.
@AussieinthePNW, @Wen, sic;

Sub vestigia photographica ab aestate proxima sedens, hiking cum @AussieinthePNW.

@Jasper_Holton, likes this,
© Uglek, 2022