[Solved] Convert Number to Letters

Discuss the spreadsheet application
Post Reply
stanmarsh
Posts: 30
Joined: Sun Dec 20, 2009 10:25 am

[Solved] Convert Number to Letters

Post by stanmarsh »

hello,

i like to convert numbers to specific letters, these are used as product code, i have seen the number to words http://user.services.openoffice.org/en/ ... =9&t=45719, maybe it can be converted to use the code below,

the code is:

A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
J = 0
K = if the number is equal to the last digit

sample product number 1000 = AJKJ = product code
product number 288933384 = BHKICKCHD = product code (the 333 = CKC not CCC)
product number 3339440-3392 = CKCIDDJ-CKIB = product code (sometimes we use "-")

thanks OOo!
Last edited by stanmarsh on Mon Dec 05, 2011 4:11 pm, edited 1 time in total.
OpenOffice 3.1 on Windows XP
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: Convert Number to Letters

Post by squenson »

There is a typo:
3339440-3392 = CKCIDKJ-CKIB

Here is a formula, assuming that your product number is in A1:

Code: Select all

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"00","JK"),"11","AK"),"22","BK"),"33","CK"),"44","DK"),"55","EK"),"66","FK"),"77","GK"),"88","HK"),"99","IK"),"0","J"),"1","A"),"2","B"),"3","C"),"4","D"),"5","E"),"6","F"),"7","G"),"8","H"),"9","I")
LibreOffice 4.2.3.3. on Ubuntu 14.04
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Convert Number to Letters

Post by Charlie Young »

This is certainly no simpler than Squenson's solution, but I've been playing with it, and I THINK it's working.

We will need a function to concatenate all the elements in an array

Code: Select all

Function Joiner(r) as String

	Dim i as Integer
	Dim j as Integer
	Dim Size(2) as Integer
	Dim s as String
	
	s = ""
	if IsArray(r) then
		Size(1) = UBound(r,1)-LBound(r,1)
		Size(2) = UBound(r,2)-LBound(r,2)
		
		s = ""
		for i = 0 to Size(1)
			for j = 0 to Size(2)
				s = s & r(LBound(r, 1)+i, LBound(r, 2) + j)
			Next j
		Next i
	else
		s = r
	endif
	Joiner = s
end Function
Then an array formula to convert the numerical code in A1 is

Code: Select all

=JOINER(IF(NOT(ISERR(SEARCH("[-\.]";MID(A1;TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))));1))));MID(A1;TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))));1);CHOOSE(VALUE(IF(TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))))>1;IF(MID(A1; :D TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))));1)=MID(A1;TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))))-1;1);"10";MID(A1;TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))));1));MID(A1;TRANSPOSE(ROW(INDIRECT("A1:"&ADDRESS(LEN(A1);1;4))));1)))+1;"J";"A";"B";"C";"D";"E";"F";"G";"H";"I";"K")))
A spreadsheet is attached for your amusement, and simplifications are invited. :D
 Edit: Just noticed I have a problem where the "333" should be "CKC" and not "CKK", stay tuned. 
Attachments
lettercodes.ods
Numbers to Letters
(10.24 KiB) Downloaded 292 times
Last edited by Charlie Young on Mon Dec 05, 2011 2:51 am, edited 1 time in total.
Apache OpenOffice 4.1.1
Windows XP
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Convert Number to Letters

Post by acknak »

This doesn't help if you have to do the job in a spreadsheet, but somewhat more direct approaches do exist...
  • $ perl -pe 'tr/1-90/A-J/; s/(.)\1/$1K/g'
    1000
    AJKJ
    288933384
    BHKICKCHD
    3339440-3392
    CKCIDKJ-CKIB
I'm sure the same tools are available in python, which can be accessed from a Calc formula.
AOO4/LO5 • Linux • Fedora 23
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Convert Number to Letters

Post by Charlie Young »

acknak wrote:... but somewhat more direct approaches do exist...

I'm sure the same tools are available in python, which can be accessed from a Calc formula.
Yes, and at some point a macro becomes preferable to a formula (though I haven't quite given up yet).

Maybe

Code: Select all

Function lettercodes(r) as String

	Dim i as Integer
	Dim s as String
	Dim m As String
	Dim v As Long
	Dim c As String
	
	s = ""
	for i = 1 to len(r)
		m = mid(r,i,1)
		if InStr("0123456789",m) > 0 then
			v = Val(m) + 1
			c = mid("JABCDEFGHI",v,1)
		else
			c = m
		endif
		if i > 1 then
			m = right(s,1)
			if c <> m then
				s = s & c
			else
				s = s & "K"
			endif
		else
			s = s & c
		endif
	next i
		
	lettercodes = s
end Function

Apache OpenOffice 4.1.1
Windows XP
stanmarsh
Posts: 30
Joined: Sun Dec 20, 2009 10:25 am

Re: Convert Number to Letters

Post by stanmarsh »

@squenson, Charlie Young and acknak

thanks so much for the help! :super: :super: :super:

i tried the formula of squenson and it was the best solution so far,

@charlie young

thanks for the help with the macro, i like to use the macro but i never had the patience to learn it! :crazy: i tried the example attachment and i also saw the 333 as CKK not CKC. i use the second script, dont know if i did it right because i just remove the text in joiner and replaced it with the your new code but im getting
BASIC runtime error
Argument in not optional

and the arrow pointed to for i = 0 to len(r)

@acknak
thanks for the help, i dont know how to use it though, can i use that in bash? where can i use that? :knock:
OpenOffice 3.1 on Windows XP
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Convert Number to Letters

Post by Charlie Young »

stanmarsh wrote:@squenson, Charlie Young and acknak

thanks so much for the help! :super: :super: :super:

i tried the formula of squenson and it was the best solution so far,

@charlie young

thanks for the help with the macro, i like to use the macro but i never had the patience to learn it! :crazy: i tried the example attachment and i also saw the 333 as CKK not CKC. i use the second script, dont know if i did it right because i just remove the text in joiner and replaced it with the your new code but im getting
BASIC runtime error
Argument in not optional

and the arrow pointed to for i = 0 to len(r)

@acknak
thanks for the help, i dont know how to use it though, can i use that in bash? where can i use that? :knock:
If I had seen the shortcoming of my array formula at first, I wouldn't have posted it, and I wouldn't recommend spending time on it to anyone who hasn't had a good bit of experience with complicated expressions, unless they get some grasp of what I'm trying to do there. I would be happy to discuss it though.

I did subsequently try to refine it, and just when I thought I was approaching success, I pasted what I thought was going to be about the last crucial piece and got the error 512 (formula overflow). I suspect what I'm trying to do with that just can't be done in one cell.

My lettercodes function is a different story. To use that, don't replace Joiner with it, just put it into a module as an independent routine. You just use it like a regular spreadsheet function pointing to some Cell containing one of your codes, say A1:

Code: Select all

=LETTERCODES(A1)
And it should also work with

Code: Select all

=LETTERCODES("3339440-3392")
which indeed gives me CKCIDKJ-CKIB.
Apache OpenOffice 4.1.1
Windows XP
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Convert Number to Letters

Post by acknak »

stanmarsh wrote:@acknak
thanks for the help, i dont know how to use it though, can i use that in bash? where can i use that?
You can use it on any computer with Perl installed, but I only meant to point out that the problem is much easier to solve with a different tool. It wasn't really offered as a practical solution.
AOO4/LO5 • Linux • Fedora 23
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: [Solved] Convert Number to Letters

Post by squenson »

i tried the formula of squenson and it was the best solution so far,
I am an :ugeek:
LibreOffice 4.2.3.3. on Ubuntu 14.04
VISS
Posts: 2
Joined: Sat Oct 13, 2012 1:22 am

Re: [Solved] Convert Number to Letters

Post by VISS »

I've been looking for a while to do a similar job.
I need a formula to convert letters in to numbers (numeric alphabet / phone) although both the numbertext extension and the formula's listed here don't work
OpenOffice 3 on Windows Vista
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: [Solved] Convert Number to Letters

Post by acknak »

It might help if you could give us an example or two: what you put in and what you want to get out.
AOO4/LO5 • Linux • Fedora 23
VISS
Posts: 2
Joined: Sat Oct 13, 2012 1:22 am

Re: [Solved] Convert Number to Letters

Post by VISS »

Sure,

When i try the formula's in this topic (and others)
I paste the formula into the cell in calc, i get 501 ERROR message.

Then i tried downloading the Number-Text formula and tried editing that to suite, it keeps translating numbers into words (literally)
All i want is that when i for example insert:
NA1BBK = 621225
According to numerical telephone alphabet

I've also tried:

Code: Select all

    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"00","JK"),"11","AK"),"22","BK"),"33","CK"),"44","DK"),"55","EK"),"66","FK"),"77","GK"),"88","HK"),"99","IK"),"0","J"),"1","A"),"2","B"),"3","C"),"4","D"),"5","E"),"6","F"),"7","G"),"8","H"),"9","I")
OpenOffice 3 on Windows Vista
User avatar
karolus
Volunteer
Posts: 1232
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Convert Number to Letters

Post by karolus »

Hallo
You want map the chars from Phone-keypad to their Numbers ? →

Code: Select all

Function phone_key_mapping( word ) as String

	oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
	map = array(array("_","a","d","g","j","m","p","t","w"))
	dim args(1) as variant
	args(1) =map
	number = ""
	for i = 1 to len( word )
		m = mid( word ,i,1)
		if instr("abcdefghijklmnopqrstuvwxyz", m) then
			args(0) = m 
			res =  oFunctionAccess.callFunction( "MATCH", args() )       
			number = number & res
		else
			number = number & m
		end if
	next i
      
	phone_key_mapping = number
	
end Function
edit: simplified the map's nestet array
Karolus
Last edited by karolus on Tue Oct 16, 2012 8:25 pm, edited 1 time in total.
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (Bookworm) (on RaspberryPI5)
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: [Solved] Convert Number to Letters

Post by squenson »

One of the reasons the formula may not work is that you are using a separator for arguments, in the formula, which is different from mine, the comma. I attach a file that should automatically adapt to your settings.
Attachments
substitute.ods
(9.42 KiB) Downloaded 252 times
LibreOffice 4.2.3.3. on Ubuntu 14.04
User avatar
karolus
Volunteer
Posts: 1232
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Convert Number to Letters

Post by karolus »

Hallo

@squenson:
VISS don't ask the same question as the OP.
He want convert for Example:
NA1BBK to 621225
Look at the keys of your mobile-phone and you will realize what it means.

( And if you want you can solve it with 26 nested SUBSTITUTE's :D )

Karolus
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (Bookworm) (on RaspberryPI5)
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: [Solved] Convert Number to Letters

Post by squenson »

I know! My point was just to confirm that my formula works as it is. I always test my formulas before posting. I then let him tune it with the 26 SUBSTITUTE :ucrazy:
LibreOffice 4.2.3.3. on Ubuntu 14.04
Post Reply