-2

Question.
I need to format string as phone numbers. I am using the function

Convert.ToInt64(Number).ToString("###-###-####")    

which works, unless I have 0000000000, all zeros.

Any suggestions on how to format all zeros as the format "###-###-####"

juharr
  • 31,741
  • 4
  • 58
  • 93
  • Does this happen if there are any leading zeros? Also why would you ever need to format all 0s to a telephone number? – user3483203 Mar 29 '18 at 16:48
  • You have `0000000000` as phone-number? – MakePeaceGreatAgain Mar 29 '18 at 16:48
  • is it that u want a (-) after every 3 letter ? – Software Dev Mar 29 '18 at 16:49
  • 1
    Minus one for lack of research effort. This question or something like it has been asked a bazillion times. – rory.ap Mar 29 '18 at 16:50
  • 1
    I'd probably skip the conversion to `long` and format the string instead. – juharr Mar 29 '18 at 16:51
  • 4
    `.ToString("000-000-0000");` Use `0` instead of `#` as a placeholder will include the a zero if the digit is missing. But it really doesn't make sense to convert a phone number to a number in the first place exactly because of leading zeros. – Matt Burland Mar 29 '18 at 16:52
  • If `Number` comes from a textbox,the solution is very easy,then u won't even need to do any formatting at all :) , do u want me to post the answer ? – Software Dev Mar 29 '18 at 16:53
  • @zackraiyan do you think that the OP has asked a question but don't want an answer? – Steve Mar 29 '18 at 16:55
  • @Steve,in my comment , i asked if he's using a textbox or not,If i post an answer regarding a textbox, he might downvote me if he's not using a textbox :( – Software Dev Mar 29 '18 at 16:55
  • Did you search for format-placeholders? https://learn.microsoft.com/dotnet/standard/base-types/custom-numeric-format-strings. Use `0` instead of `#` when you want to display (leading) zeros also. – MakePeaceGreatAgain Mar 29 '18 at 16:57
  • This question has already been answered. Please do at least a little searching online first. – Rufus L Mar 29 '18 at 16:57
  • Yes I have 0000000000 as a phone number and it has to be formatted that way, that is the requirement. It is an old system I have to match exactly what the old system does. – dconner2006 Mar 29 '18 at 17:00
  • `$"{Number.Substring(0, 3)}-{Number.Substring(3, 3)}-{Number.Substring(6)}"` – Rufus L Mar 29 '18 at 17:12

1 Answers1

3

The short answer is use:

.ToString("000-000-0000"); 

Using 0 instead of # as a placeholder will include the a zero if the digit is missing.

But the longer answer is that it really doesn't make sense to convert a phone number from a string to a number in the first place exactly because of leading zeros. The same applies to lots of other "numbers" that aren't really numbers, but are codes instead. Like zip codes for example.

If Number is a string and is coming from a user input, you also have the problem that the user might insert - already. Or even brackets. So you might be approaching this entirely the wrong way to start with.

A better solution would be to use a regex to extract and then format the numerical digits from your string. For that see:

https://stackoverflow.com/a/188607

For example.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171