Can I format a string as a number in .NET?

If I had a phone number like this

string phone = "6365555796"; 

What I only store with numeric characters in my database (as a string) , is it possible to display the number as follows:

 "636-555-5796" 

Like me, if I were to use a number:

 long phone = 6365555796; string output = phone.ToString("000-000-0000"); 

I tried searching, and all I could find on the Internet was numeric formatting documents.

The reason I'm asking for is because I think it would be an interesting idea to be able to store only numeric values ​​in the database and allow different formatting using the Constant string value to dictate how my phone numbers are formatted. Or am I better off using a number for this?

EDIT: The question is about formatting a string containing numbers, not the number itself.

+4
source share
9 answers

The best I can think of without having to convert it to a long / number, and therefore it is suitable for a single line:

 string number = "1234567890"; string formattedNumber = string.Format("{0}-{1}-{2}", number.Substring(0,3), number.Substring(3,3), number.Substring(6)); 
+7
source

Remember that not everyone uses the North American 3-3-4 format for phone numbers. European phone numbers can be up to 15 digits long with substantial punctuation, for example. + 44-XXXX-XXXX-XXXX is different from 44 + XXXX-XXXX-XXXX. You also do not consider PBXs and extensions that may require more than 30 digits.

Military and cordless phones may have alphabetic characters, no, this is not the "2" = "ABC" that you see on touch screen phones.

+6
source

Simple version:

 string phone = "6365555796"; Convert.ToInt64(phone).ToString("000-000-0000"); 

To wrap some confirmation around this and put it in a good method:

 string FormatPhone(string phone) { /*assume the phone string came from the database, so we already know it's only digits. If this changes in the future a simple RegEx can validate (or correct) the digits requirement. */ // still want to check the length: if (phone.Length != 10) throw new InvalidArgumentException(); return Convert.ToInt64(phone).ToString("000-000-0000"); } 
+6
source

I think it works

 string s = string.Format("{0:###-###-####}", ulong.Parse(phone)); 

In addition, this http://blog.stevex.net/index.php/string-formatting-in-csharp/ is a good .NET line formatting post.

Thanks @swilliams for the clarification.

+4
source

Why not just do something like that?

 string phoneText = "6365555796"; long phoneNum = long.Parse(phoneText); string output = phoneNum.ToString("000-000-0000"); 
+1
source

I like some kind of extension method action:

  /// <summary> /// Formats a string of nine digits into a US phone number. /// </summary> /// <param name="value"> /// The string to format as a phone number. /// </param> /// <returns> /// The numeric string as a US phone number. /// </returns> public static string ToUSPhone (this string value) { if (value == null) { return null; } long dummy; if ((value.Length != 10) || !long.TryParse ( value, NumberStyles.None, CultureInfo.CurrentCulture, out dummy)) { return value; } return string.Format ( CultureInfo.CurrentCulture, "{0}-{1}-{2}", value.Substring (0, 3), value.Substring (3, 3), value.Substring (6)); } 
+1
source

You could potentially insert a string at the given point of the character. A kind of: phone = phone.Insert (3, "-"); phone = phone.Insert (7, "-");

0
source

Sure:

 Regex.Replace(phone, @"^(\d{3})(\d{3})(\d{4})$", @"$1-$2-$3") 

Of course, now you have 2 problems .

0
source

To control the string format of a number to display in an ASP.NET Gridview or other control, you can wrap your element in a helper class for display purposes:

 public class PhoneDisplay { long phoneNum; public PhoneDisplay(long number) { phoneNum = number; } public override string ToString() { return string.Format("{0:###-###-####}", phoneNum); } } 

Then define the data or display fields as (for example):

 PhoneDisplay MyPhone = new PhoneDisplay(6365555796); 
-1
source

All Articles