Use FormatOutOfCountry to get the prefix for the caller’s region: 011 from the US or 00 from Germany for this UK number.
dotnet add package RecursiveAI.OneContactRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneContact; var phone = PhoneNumber.Parse( "+44 20 7946 0123"); foreach (var from in new[] { "US", "DE" }) { var dial = phone.FormatOutOfCountry( Region.Parse(from)); Console.WriteLine($"{from}: {dial}"); }
Unzip the example, then dotnet run.
Prints:
US: 011 44 20 7946 0123 DE: 00 44 20 7946 0123
The number in the example is from Ofcom’s range for drama. If a parsed caller region has no metadata, FormatOutOfCountry returns the international + form rather than guessing a dialing prefix.
Variations
Dial the same number from within its own calling-code area. Add this after the loop:
var gb = Region.Parse("GB"); Console.WriteLine( phone.FormatOutOfCountry(gb));
020 7946 0123Keep the international form for readers in any region. Add this after the loop instead:
Console.WriteLine( phone.Format(PhoneFormat.International)); Console.WriteLine(phone.E164);
+44 20 7946 0123 +442079460123
Related recipes
The short answer
Parse the destination number once, then call FormatOutOfCountry with the region the call originates from. Here 44 is the destination country calling code in both results. Only the prefix before it changes: 011 for a call from the US, 00 for a call from Germany. The formatter produces a string; it doesn’t place a call.
How do I format a phone number based on the country?
There are two regions to think about. If your input is a national number like 020 7946 0123, the destination region tells the parser how to read it. If the input already begins with +44, the country calling code is in the input. Either way, the caller’s region goes to FormatOutOfCountry. Don’t use the destination region for both steps just because you know it first.
The formatting rules prefer a region’s designated international prefix when it has one. Otherwise, OneContact uses a single plain prefix when available and falls back to international format when it can’t select one. Inside the same calling-code area, the formatter returns national format; inside the North American Numbering Plan it puts 1 before the national format. This is region-based formatting, not a test of whether a carrier or phone can connect the call.
How do I format my phone number for international use?
If you’re publishing a number for readers in several countries, use PhoneFormat.International. It keeps the + instead of assuming everyone uses 011 or 00. For a compact value in your contact database, E164 keeps the +, country calling code and national digits without spaces. Keep an extension separately if you need it. Generate a caller-specific string only when you know the calling region and want to show the prefix used there.