How to convert number to english?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert number to english?
# 1  
Old 02-14-2015
How to convert number to english?

Hi gurus,

I have a weird requirement. I need to convert the number to english lecture.

I have 1.2 ....19 numbers
I need to convert to first second third fourth, fifth, sixth...

Is there any way convert it using unix command?


thanks in advance.
# 2  
Old 02-14-2015
Some more context, please. Do you want ordinal (as posted) or cardinal numbers? What tool/system/language do you need this for? Show input an desired output.
# 3  
Old 02-14-2015
Quick'n'dirty...
Longhand using OSX 10.7.5, default bash terminal.
Code:
#!/bin/bash
# numalpha.sh
alpha=""
n=1
numalpha()
{
	if [ $n -eq 1 ]
	then
		alpha="first"
	fi
	if [ $n -eq 2 ]
	then
		alpha="second"
	fi
	if [ $n -eq 3 ]
	then
		alpha="third"
	fi
	if [ $n -eq 4 ]
	then
		alpha="fourth"
	fi
	# And so on...
}
# Test for integers numbers only.
for n in {1..4}
do
	numalpha
	echo "$n = $alpha"
done

Results for 4 values only.
Code:
Last login: Sat Feb 14 21:06:21 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 numalpha.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./numalpha.sh
1 = first
2 = second
3 = third
4 = fourth
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 4  
Old 02-14-2015
Hi,
With bash, it's more easy to use array,example ($ in begin line is my prompt):
Code:
$ nblexical=("zero" "first" "second" "third")
$ nb=2
$ echo ${nblexical[$nb]} 
second

Regard.
# 5  
Old 02-14-2015
Same machine...
Code:
#!/bin/bash
# numalpha.sh
alpha=""
numalpha()
{
	if [ $1 -eq 1 ]
	then
		alpha="first"
	fi
	if [ $1 -eq 2 ]
	then
		alpha="second"
	fi
	if [ $1 -eq 3 ]
	then
		alpha="third"
	fi
	if [ $1 -eq 4 ]
	then
		alpha="fourth"
	fi
	# And so on...
}
# Test for integer numbers only.
text=( This is the 1 and only attempt to change the 4 word inside this string. )
count=0
while [ $count -lt ${#text[@]} ]
do
	if [[ ${text[$count]} =~ ^-?[0-9]+$ ]]
	then
		numalpha "${text[$count]}"
		printf "$alpha "
		count=$((count+1))
	fi
	printf "${text[$count]} "
	alpha=""
	count=$((count+1))
done

Results:-
Code:
Last login: Sat Feb 14 22:54:01 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./numalpha.sh
This is the first and only attempt to change the fourth word inside this string.
AMIGA:barrywalker~/Desktop/Code/Shell> _


Last edited by wisecracker; 02-14-2015 at 07:43 PM..
# 6  
Old 02-16-2015
For a US English translation of the integers in the range from 0 up to and including 999999999999999999999999999999999999 into ordinal numbers, you could try something like:
Code:
#!/bin/ksh
awk '
BEGIN {	# Initialize variables...
	# Final single digits:
	fd[0] = "zeroth"; fd[1] = "first"; fd[2] = "second"; fd[3] = "third"
	fd[4] = "fourth"; fd[5] = "fifth"; fd[6] = "sixth"; fd[7] = "seventh"
	fd[8] = "eighth"; fd[9] = "ninth"
	# Leading single digits:
	ld[1] = "one"; ld[2] = "two"; ld[3] = "three"; ld[4] = "four"
	ld[5] = "five"; ld[6] = "six"; ld[7] = "seven"; ld[8] = "eight"
	ld[9] = "nine"
	# Final teens:
	ft[10] = "tenth"; ft[11] = "eleventh"; ft[12] = "twelfth"
	ft[13] = "thirteenth"; ft[14] = "fourteenth"; ft[15] = "fifteenth"
	ft[16] = "sixteenth"; ft[17] = "seventeenth"; ft[18] = "eighteenth"
	ft[19] = "nineteenth"
	# Leading teens:
	lt[10] = "ten"; lt[11] = "eleven"; lt[12] = "twelve"
	lt[13] = "thirteen"; lt[14] = "fourteen"; lt[15] = "fifteen"
	lt[16] = "sixteen"; lt[17] = "seventeen"; lt[18] = "eighteen"
	lt[19] = "nineteen"
	# Final tens:
	fT[2] = "twentieth"; fT[3] = "thirtieth"; fT[4] = "fortieth"
	fT[5] = "fiftieth"; fT[6] = "sixtieth"; fT[7] - "seventieth"
	fT[8] = "eightieth"; fT[9] = "ninetieth"
	# Leading tens:
	lT[2] = "twenty"; lT[3] = "thirty"; lT[4] = "forty"; lT[5] = "fifty"
	lT[6] = "sixty"; lT[7] = "seventy"; lT[8] = "eighty"; lT[9] = "ninety"
	# Units:
	u[2] = "thousand"; u[3] = "million"; u[4] = "billion"; u[5] = "trillion"
	u[6] = "quadrillion"; u[7] = "quintillion"; u[8] = "sextillion"
	u[9] = "septillion"; u[10] = "octillion"; u[11] = "nonillion"
	u[12] = "decillion"; u[13] = "undecillion"
	# The last entry above will only be used in overflow diagnostics.  If
	# more entries are added, remember that one extra entry must be added.
	# The following maximum u[] subscript must be updated if more entries
	# are added above.
	ucnt = 13
}
# Function to print US English string corresponding to 3 digit numeric string.
function p3(units, gcnt, gnum,    d1, d2, d3, d23) {
	# If we have a zero and this is not the last group, nothing to print...
	if(g[gnum] == 0 && gnum < gcnt) return(1)
	# Grab inividual digits and last two digits...
	d1 = int(g[gnum] / 100)
	d23 = g[gnum] % 100
	d2 = int(d23 / 10)
	d3 = d23 % 10
	# Hundreds to print?
	if(d1)
		printf("%s hundred%s", ld[d1],
			d23 ? " " : (gcnt == gnum) ? "th\n" : " " units \
				(t[gnum] ? " " : "th\n"))
	# Print last two digits...
	if(d23 || (d1 == 0 && gnum == gcnt))
		if(d2 == 1) 
			# 10-19:
			printf("%s", (gnum == gcnt) ? ft[d23] "\n" : \
				lt[d23] " " units (t[gnum] ? " " : "th\n"))
		else if(d2)
			# 20-99:
			if(d3)	# [2-9][1-9]:
				printf("%s-%s", lT[d2],
					(gnum == gcnt) ? fd[d3] "\n" : \
					ld[d3] " " units \
					(t[gnum] ? " " : "th\n"))
			else	# [2-9]0:
				printf("%s", (gnum == gcnt) ? fT[d2] "\n" : \
					lT[d2] " " units \
					(t[gnum] ? " " : "th\n"))
		else	# 0-9:
			printf("%s", (gnum == gcnt) ? fd[d3] "\n" : ld[d3] " " \
				units (t[gnum] ? " " : "th\n"))
	return(t[gnum])
}
# Process the first field from each line in an input file...
{	# Show original input...
	printf("Input:\"%s\"\n", $0)
	# Check for non-digits
	if(match($1, /[^[:digit:]]/)) {
		print "Only digits are alloweed."
		next
	}
	# Strip leading 0s
	if(match($1, /^0+/)) {
		$1 = (RLENGTH == length($1)) ? "0" : substr($1, RLENGTH + 1)
		printf("Updated input:\"%s\"\n", $0)
	}
	# Split into groups of three digits...
	ng = int((length($1) + 2) / 3)
	if(ng == 0) next	# skip eimpty lines
	if(ng >= ucnt) {	# Too big to handle?
		printf("Can only handle numbers less than one %s.\n",
			u[ucnt])
		next
	}
	gw = length($1) - (ng - 1) * 3
	off = 1
	for(i = 1; i <= ng; i++) {
		g[i] = substr($1, off, gw)
		t[i] = substr($1, off + gw) + 0
		off += gw
		gw = 3
	}
	# Process the groups of digits...
	for(i = 1; p3(u[ng + 1 - i], ng, i); i++);
}' file

This awk script will only convert the 1st field in each line read from a file named file. You can easily change this to read from standard input or another file and, without too much work, it could process a different set of fields from each input line.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk. This was written and tested on OS X 10.10.2.

With file containing:
Code:
0
1
11
111
123000000456000000789
1000000000000000000000023
2034500
975310000000000000000000000987654321
1000000000000000000000000000000000000

the output produced is:
Code:
Input:"0"
Updated input:"0"
zeroth
Input:"1"
first
Input:"11"
eleventh
Input:"111"
one hundred eleventh
Input:"123000000456000000789"
one hundred twenty-three quintillion four hundred fifty-six billion seven hundred eighty-ninth
Input:"1000000000000000000000023"
one septillion twenty-third
Input:"2034500"
two million thirty-four thousand five hundredth
Input:"975310000000000000000000000987654321"
nine hundred seventy-five decillion three hundred ten nonillion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-first
Input:"1000000000000000000000000000000000000"
Can only handle numbers less than one undecillion.

If you need to extend the range of numbers accepted, you can find higher order names here: Wikipedia: Names of large numbers.
These 8 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-17-2015
A perl alternative

Hi.

For Don's data file, augmented, in file data2:"
Code:
0
1
11
111
1,011
1_012
123000000456000000789
1000000000000000000000023
2034500
975310000000000000000000000987654321
1000000000000000000000000000000000000

the perl code:
Code:
perl -M"Lingua::EN::Numbers qw(num2en num2en_ordinal)" -n -e 'chomp;
print " Cardinal for $_: ",num2en($_),"\n";
print " Ordinal  for $_: ", num2en_ordinal($_),"\n";' data2

produces:
Code:
 Cardinal for 0: zero
 Ordinal  for 0: zeroth
 Cardinal for 1: one
 Ordinal  for 1: first
 Cardinal for 11: eleven
 Ordinal  for 11: eleventh
 Cardinal for 111: one hundred and eleven
 Ordinal  for 111: one hundred and eleventh
 Cardinal for 1,011: one thousand and eleven
 Ordinal  for 1,011: one thousand and eleventh
 Cardinal for 1_012: 
 Ordinal  for 1_012: 
 Cardinal for 123000000456000000789: one hundred and twenty-three quintillion, four hundred and fifty-six billion, seven hundred and eighty-nine
 Ordinal  for 123000000456000000789: one hundred and twenty-three quintillion, four hundred and fifty-six billion, seven hundred and eighty-ninth
 Cardinal for 1000000000000000000000023: one septillion and twenty-three
 Ordinal  for 1000000000000000000000023: one septillion and twenty-third
 Cardinal for 2034500: two million, thirty-four thousand, five hundred
 Ordinal  for 2034500: two million, thirty-four thousand, five hundredth
 Cardinal for 975310000000000000000000000987654321: nine hundred and seventy-five times ten to the thirty-third, three hundred and ten nonillion, nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one
 Ordinal  for 975310000000000000000000000987654321: nine hundred and seventy-five times ten to the thirty-third, three hundred and ten nonillion, nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-first
 Cardinal for 1000000000000000000000000000000000000: one times ten to the thirty-sixth
 Ordinal  for 1000000000000000000000000000000000000: one times ten to the thirty-sixthth

As noted in How can I convert a number to its English form in Perl? - Stack Overflow

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

2. Shell Programming and Scripting

Convert a String to a Number

I read in two numbers from a user but the number is a string. #!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 I know you can use the the "expr" or "bc" command to automatically convert the string to a number then add them together. But I don't want to add... (10 Replies)
Discussion started by: Loc
10 Replies

3. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies

4. Shell Programming and Scripting

Convert number

friends as I can convert this value to number in this example it is well but can vary the value does not help me cut from a nesecito espefifica opsiocn get zero CantCabe = 00023 Cant = 23 CantPntCabe = 0000000000000034 CantPnt = 34 if && ; then echo... (3 Replies)
Discussion started by: tricampeon81
3 Replies

5. Programming

SED - how do I convert a decimal number to asterisk

Hi, My animal ID's have two zeros in them and are also converting to asterisk. I only need to change the zero values in columns two and three. I would appreciate any help. This is my data structure: head phendata.txt 201008809 0.0 0.0 201008810 0.0 0.0 201008813 0.0 0.0 201014103... (6 Replies)
Discussion started by: lel7lel7
6 Replies

6. Shell Programming and Scripting

convert english to chinese

Hi Experts, Can anyone help me to convert a english input into chinese in a bash script. help would be highly appreciable. thanks, Deepak (3 Replies)
Discussion started by: naw_deepak
3 Replies

7. Shell Programming and Scripting

Convert e+ to number

Hi, I am using awk to get particular dates in seconds and the output am getting is like 1.28071e+09. How can I convert it to number format. Can anyone help me out? Thanks in advance..! (7 Replies)
Discussion started by: Kattoor
7 Replies

8. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

9. Shell Programming and Scripting

How to convert English text file to ASCII File?

file abc abc: English text I want to convert the above into file abc file: ascii text (1 Reply)
Discussion started by: laknar
1 Replies

10. Shell Programming and Scripting

Convert ip address to ip number

I want to make a script to read a list of ip addresses from a file then convert those ip addresses to ip number. ip number is made by removing the dots then transfer to a number , so the ip number transfered to binary then to decimal which will represents the ip number 0 : 4294967295 Also I... (17 Replies)
Discussion started by: moutaz1983
17 Replies
Login or Register to Ask a Question