Formatting
Format parsed numbers with NF.Format. Prefer formatting after NF.isValid when persisting data.
NF.format
Formats a phone number as a string in the chosen format.
Signatures
global static String format(NF.Phone phone, NF.Format formatValue)Parameters
Returns
Formatted string.
Throws
None for typical valid numbers; invalid numbers may format unpredictably; validate first.
libphonenumber
PhoneNumberUtil.format(PhoneNumber, PhoneNumberFormat)
Example
NF.Phone phone = NF.parse(contact.Phone, 'US');contact.Phone = NF.format(phone, NF.Format.E164);// → "+16502530000"NF.formatByPattern
Formats using custom pattern rules instead of region metadata defaults.
Signatures
global static String formatByPattern(NF.Phone phone, NF.Format formatValue, List<NF.NumberFormatRule> rules)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Parsed number. |
formatValue | NF.Format | Base format context. |
rules | List<NF.NumberFormatRule> | Custom rules from NF.Metadata (NF.metadataForRegion). |
Returns
Formatted string.
Throws
None.
libphonenumber
PhoneNumberUtil.formatByPattern(PhoneNumber, PhoneNumberFormat, List<NumberFormat>)
Example
NF.Metadata us = NF.metadataForRegion('US');List<NF.NumberFormatRule> rules = us.numberFormats();String out = NF.formatByPattern(phone, NF.Format.NATIONAL, rules);// → national format using US metadata rulesNF.formatInto
Writes formatted output into an NF.MutableText buffer.
Signatures
global static NF.MutableText formatInto(NF.Phone phone, NF.Format formatValue, NF.MutableText target)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Parsed number. |
formatValue | NF.Format | Target format. |
target | NF.MutableText | Output buffer; created if null. |
Returns
The same NF.MutableText with formatted text in value().
Throws
None.
libphonenumber
PhoneNumberUtil.format(PhoneNumber, PhoneNumberFormat, StringBuilder)
Example
NF.MutableText buf = new NF.MutableText();NF.formatInto(phone, NF.Format.INTERNATIONAL, buf);String display = buf.value();NF.formatNationalNumberWithCarrierCode
Formats the national number with an explicit domestic carrier code.
Signatures
global static String formatNationalNumberWithCarrierCode(NF.Phone phone, String carrierCode)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Parsed number. |
carrierCode | String | Carrier selection code for the region. |
Returns
Formatted national string.
Throws
None.
libphonenumber
PhoneNumberUtil.formatNationalNumberWithCarrierCode(PhoneNumber, String)
Example
String national = NF.formatNationalNumberWithCarrierCode(phone, '1');NF.formatNationalNumberWithPreferredCarrierCode
Formats with carrier code from the number, or a fallback.
Signatures
global static String formatNationalNumberWithPreferredCarrierCode(NF.Phone phone, String fallbackCarrierCode)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Parsed number. |
fallbackCarrierCode | String | Used when the number has no preferred carrier. |
Returns
Formatted national string.
Throws
None.
libphonenumber
PhoneNumberUtil.formatNationalNumberWithPreferredCarrierCode(PhoneNumber, String)
Example
String national = NF.formatNationalNumberWithPreferredCarrierCode(phone, '1');NF.formatNumberForMobileDialing
Formats for dialling from a mobile phone in a given region.
Signatures
global static String formatNumberForMobileDialing(NF.Phone phone, String regionCallingFrom, Boolean withFormatting)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Number to dial. |
regionCallingFrom | String | Region where the call originates. |
withFormatting | Boolean | Whether to include formatting characters. |
Returns
Dial string suitable for mobile use.
Throws
None.
libphonenumber
PhoneNumberUtil.formatNumberForMobileDialing(PhoneNumber, String, boolean)
Example
String dial = NF.formatNumberForMobileDialing(phone, 'US', false);NF.formatOutOfCountryCallingNumber
Formats how to dial this number from another country.
Signatures
global static String formatOutOfCountryCallingNumber(NF.Phone phone, String regionCallingFrom)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Destination number. |
regionCallingFrom | String | Caller’s region (e.g. CA dialling a US number). |
Returns
Out-of-country dialling string.
Throws
None.
libphonenumber
PhoneNumberUtil.formatOutOfCountryCallingNumber(PhoneNumber, String)
Example
String fromCanada = NF.formatOutOfCountryCallingNumber(usPhone, 'CA');NF.formatInOriginalFormat
Formats using the original format style when raw input was preserved.
Signatures
global static String formatInOriginalFormat(NF.Phone phone, String regionCallingFrom)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Number (ideally from NF.parseAndKeepRawInput). |
regionCallingFrom | String | Region context for formatting. |
Returns
Formatted string in original style.
Throws
None.
libphonenumber
PhoneNumberUtil.formatInOriginalFormat(PhoneNumber, String)
Example
NF.Phone phone = NF.parseAndKeepRawInput(userInput, 'US');String display = NF.formatInOriginalFormat(phone, 'US');NF.formatOutOfCountryKeepingAlphaChars
Out-of-country format that preserves alphabetic characters (vanity numbers).
Signatures
global static String formatOutOfCountryKeepingAlphaChars(NF.Phone phone, String regionCallingFrom)Parameters
| Name | Type | Description |
|---|---|---|
phone | NF.Phone | Vanity or alpha number. |
regionCallingFrom | String | Caller’s region. |
Returns
Formatted dialling string with letters kept where applicable.
Throws
None.
libphonenumber
PhoneNumberUtil.formatOutOfCountryKeepingAlphaChars(PhoneNumber, String)
Example
NF.Phone vanity = NF.parseAndKeepRawInput('1800 MICROSOFT', 'US');String dial = NF.formatOutOfCountryKeepingAlphaChars(vanity, 'US');NF.MutableText (output buffer)
Section titled “NF.MutableText (output buffer)”Used with NF.formatInto. Not a phone number; a reusable string holder.
| Method | Returns | Description |
|---|---|---|
MutableText() / MutableText(String) | (none) | Construct empty or with initial text. |
append(String) | NF.MutableText | Append text. |
clear() | NF.MutableText | Clear contents. |
value() / toString() | String | Current text. |
setValue(String) | NF.MutableText | Replace contents. |
length() | Integer | Character length. |
libphonenumber: StringBuilder (Java)