Skip to content

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

NameTypeDescription
phoneNF.PhoneParsed number.
formatValueNF.FormatE164, INTERNATIONAL, NATIONAL, or RFC3966.

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

NameTypeDescription
phoneNF.PhoneParsed number.
formatValueNF.FormatBase format context.
rulesList<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 rules

NF.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

NameTypeDescription
phoneNF.PhoneParsed number.
formatValueNF.FormatTarget format.
targetNF.MutableTextOutput 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

NameTypeDescription
phoneNF.PhoneParsed number.
carrierCodeStringCarrier 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

NameTypeDescription
phoneNF.PhoneParsed number.
fallbackCarrierCodeStringUsed 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

NameTypeDescription
phoneNF.PhoneNumber to dial.
regionCallingFromStringRegion where the call originates.
withFormattingBooleanWhether 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

NameTypeDescription
phoneNF.PhoneDestination number.
regionCallingFromStringCaller’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

NameTypeDescription
phoneNF.PhoneNumber (ideally from NF.parseAndKeepRawInput).
regionCallingFromStringRegion 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

NameTypeDescription
phoneNF.PhoneVanity or alpha number.
regionCallingFromStringCaller’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');

Used with NF.formatInto. Not a phone number; a reusable string holder.

MethodReturnsDescription
MutableText() / MutableText(String)(none)Construct empty or with initial text.
append(String)NF.MutableTextAppend text.
clear()NF.MutableTextClear contents.
value() / toString()StringCurrent text.
setValue(String)NF.MutableTextReplace contents.
length()IntegerCharacter length.

libphonenumber: StringBuilder (Java)