# Formatting phone numbers

**URL:** <https://clarionhub.com/t/formatting-phone-numbers/7311>\
**Category:** questions\
**Tags:** c9\
**Created:** [July 12, 2024, 5:52am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311 "2024-07-12T05:52:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenns](https://clarionhub.com/user_avatar/clarionhub.com/kenns/32/6428_2.png) [@kenns](https://clarionhub.com/u/kenns)\
**Post date:** [July 12, 2024, 5:52am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/1 "2024-07-12T05:52:11Z")

</div>

I have a file containing phone numbers. There is one phone number in a record, and that phone number is assigned a "type". The phone number type, PHO:PhoneType, is a byte, and is selected from a drop list with the following options:

'Mobile|#0|Work|#1|Home|#2|Work Fax|#3|Home Fax|#4|Direct|#5|Reception|#6|After Hrs.|#7|13 Number|#8|1300 Number|#9|18 Number|#10|1800 Number|#11|Other|#12'

The phone number field, PHO:Number, is a string of 20 characters.  
I have the following routine attempting (unsuccessfully) to format the displayed phone number depending on the type.

```auto
PhoneNumberFormat ROUTINE
	CASE PHO:PhoneType
	OF 0 ! Mobile
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)
	OF 1 ! Work
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 2 ! Home
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 3 ! Work Fax
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 4 ! Home Fax	
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 5 ! Direct Line
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 6 ! Reception
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 7 ! After Hours
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 8 ! 13 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-??-??P)
	OF 9 ! 1300 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)
	OF 10 ! 18 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-??-??P)
	OF 11 ! 1800 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)	
	OF 12
		PHO:Number = FORMAT(PHO:Number,@S20)
	END
	DISPLAY()

```

The routine is called when the PHO:PhoneType is Accepted.

It is my intention that the PHO:Number should be stored as a continuous string for sorting purposes, and that the Picture attribute is used only for display.

What I have currently is storing nothing in the PHO:Number field. I am using Clarion 9 with TPS files.

Any suggestions would be most welcome.

Thanks in advance,  
Kenn Sharples

---

<div class="post-metadata">

**Author:** ![seanh](https://clarionhub.com/letter_avatar_proxy/v4/letter/s/4af34b/32.png) [@seanh](https://clarionhub.com/u/seanh)\
**Post date:** [July 12, 2024, 6:26am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/2 "2024-07-12T06:26:11Z")

</div>

I’ve always had problems with trying to format phone numbers. Turns out it’s difficult and not everyone agrees. In Australia almost all our phone numbers are 10 digits. except when they aren’t. we have some special 6 digit ones. Our mobile number all have the area code of 04 and most people format that as nnnn nnn nnn but some (like me) prefer nn nnnn nnnn for everything.  
Then you have international numbers where you need to add the country code. so +nn nnn nnnn nnnn ???

That all just makes formatting and sorting challenging, so I don’t. I have a S20 string field and let my user use their prefered formatting scheme.

---

<div class="post-metadata">

**Author:** ![Marius\_Nel](https://clarionhub.com/user_avatar/clarionhub.com/marius_nel/32/2309_2.png) [@Marius\_Nel](https://clarionhub.com/u/Marius_Nel)\
**Post date:** [July 12, 2024, 8:44am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/3 "2024-07-12T08:44:29Z")

</div>

Kenn,

Look at StringTheory,

dfmt StringDeformat

sPhone = dfmt.deformatPattern(PhoneNumber,‘@p(\<\<\<###) ### ####’) ! RETURNS 0878280123

---

<div class="post-metadata">

**Author:** ![Mike\_Duglas](https://clarionhub.com/user_avatar/clarionhub.com/mike_duglas/32/1061_2.png) [@Mike\_Duglas](https://clarionhub.com/u/Mike_Duglas)\
**Post date:** [July 12, 2024, 9:05am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/4 "2024-07-12T09:05:08Z")

</div>

Seems you are using DEFORMAT in a wrong way.

---

<div class="post-metadata">

**Author:** ![Flavio\_Suarez](https://clarionhub.com/user_avatar/clarionhub.com/flavio_suarez/32/1347_2.png) [@Flavio\_Suarez](https://clarionhub.com/u/Flavio_Suarez)\
**Post date:** [July 12, 2024, 11:56am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/5 "2024-07-12T11:56:46Z")

</div>

AFAIK, pattern pictures (@PP) don’t use the ‘?’ token…

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 12, 2024, 12:11pm UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/6 "2024-07-12T12:11:59Z")

</div>

> [@kenns](#):
>
> ```auto
> PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)
> 			
> 
> ```

Use # for @P numbers and no @s20 picture with Deformat.

```auto
PHO:Number = FORMAT(DEFORMAT(PHO:Number),@P####-####-###P)
			

```

[This tool on GitHub](https://github.com/CarlTBarnes/Date-Time-Number-Tool#number-pictures--or-any-others-e-p-d-t-s) will let you test Format and Deformat with any picture so you can know how it will work without compiling.

The screenshot shows @N pictures but you can use @P as seen on the bottom right.

![image](https://clarionhub.com/images/transparent.png)

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [July 12, 2024, 1:23pm UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/7 "2024-07-12T13:23:32Z")

</div>

Since EVERYTHING you’re passing to format is the exact same DEFORMAT, you can break up your code to make it easier to test.

Your first line could be

` Deformatted = DEFORMAT(PHO:Number,@s20)`

Now you can test for the value of what you get from your DEFORMAT(). Then you’ll find that it isn’t doing what you want.

So perhaps, in your testing, you try it without the @S20 because now you’ve re-read the docs for DEFORMAT and have seen the examples.

Now you can pass your Deformatted to FORMAT with more confidence about what is transpiring, and make more maintainable code as a bonus.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [July 12, 2024, 1:39pm UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/8 "2024-07-12T13:39:22Z")

</div>

Never had a user complain about that either.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 12, 2024, 4:10pm UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/9 "2024-07-12T16:10:39Z")

</div>

> [@jslarve](#):
>
> Since EVERYTHING you’re passing to format is the exact same DEFORMAT, you can break up your code to make it easier to test.  
> …
> 
> Now you can test for the value of what you get from your DEFORMAT(). Then you’ll find that it isn’t doing what you want.

Good idea do everything by calling a  
`PhoneEntered PROCEDURE(*STRING inOutPhone, STRING inPicture) `

Then you could add features like tell the user he did not enter enough digits by counting the # in the picture.

---

<div class="post-metadata">

**Author:** ![kenns](https://clarionhub.com/user_avatar/clarionhub.com/kenns/32/6428_2.png) [@kenns](https://clarionhub.com/u/kenns)\
**Post date:** [July 13, 2024, 6:59am UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/10 "2024-07-13T06:59:46Z")

</div>

I adjusted  
`PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)`  
to read  
`PHO:Number = FORMAT(DEFORMAT(PHO:Number),@P####-####-###P)`  
in my routine, and now, all is formatting nicely.

Thank you, CarlBarnes and all others that responded.  
Regards,  
Kenn Sharples

---

<div class="post-metadata">

**Author:** ![ChrisBos](https://clarionhub.com/letter_avatar_proxy/v4/letter/c/e495f1/32.png) [@ChrisBos](https://clarionhub.com/u/ChrisBos)\
**Post date:** [July 16, 2024, 12:52pm UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/11 "2024-07-16T12:52:19Z")

</div>

When help in performing a task is requested, simply saying, “You’re doing it wrong,” is of no help at all.

---

<div class="post-metadata">

**Author:** ![Mike\_Duglas](https://clarionhub.com/user_avatar/clarionhub.com/mike_duglas/32/1061_2.png) [@Mike\_Duglas](https://clarionhub.com/u/Mike_Duglas)\
**Post date:** [July 16, 2024, 1:24pm UTC](https://clarionhub.com/t/formatting-phone-numbers/7311/12 "2024-07-16T13:24:32Z")

</div>

You’re right, I should have added “read the docs”.
