1# Phone Number Formatting 2 3## Use Cases 4 5The number of digits, combination mode, and display mode of phone numbers vary according to countries and regions. Besides, the dialing mode and number format may also differ under different situations. For example, to make a cross-area call in Chinese mainland, you usually need to type **0** and then dial the area code plus an 8-digit phone number. To make a call in Hong Kong or Macao, you need to dial the number in a different way. 6 7Through phone number formatting, your application will be able to present phone numbers to users in their expected format. 8 9## How to Develop 10 11Phone number formatting is implemented through the [format](../reference/apis-localization-kit/js-apis-i18n.md#format8) API of the [PhoneNumberFormat](../reference/apis-localization-kit/js-apis-i18n.md#phonenumberformat8) class. The development procedure is as follows: 12 131. Import the **i18n** module. 14 ```ts 15 import { i18n } from '@kit.LocalizationKit'; 16 ``` 17 182. Create a **PhoneNumberFormat** object. 19 20 You can set different phone number formats through **PhoneNumberFormatOptions**. For details, see Table 1. 21 22 ```ts 23 let phoneNumberFormat: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat(country: string, options?: PhoneNumberFormatOptions); 24 ``` 25 263. Format a phone number. 27 ```ts 28 let formattedPhoneNumber: string = phoneNumberFormat.format(phoneNumber: string); 29 ``` 30 314. Check whether the phone number is correct and obtain its home area. 32 ```ts 33 The let isValidNumber: boolean = phoneNumberFormat.isValidNumber(phoneNumber: string); // Check whether the phone number is correct. 34 let locationName: string = phoneNumberFormat.getLocationName(number: string, locale: string); // Obtain the home area of the phone number. 35 ``` 36 37**Phone Number Formatting Options** 38 39The following uses the phone number **158\*\*\*\*2312** and the country code **CN** as an example to show the values of **PhoneNumberFormatOptions** and corresponding display effects. 40 41**Table 1** Phone number formatting type (type) 42 43| Value| Display Effect| 44| -------- | -------- | 45| E164 | +86 158\*\*\*\*2312 | 46| INTERNATIONAL | +86 158 \*\*\*\* 2312 | 47| NATIONAL | 158 \*\*\*\* 2312 | 48| RFC3966 | tel:+86-158-\*\*\*\*-2312 | 49| TYPING | 158 \*\*\* | 50 51 52**Development Example** 53 54```ts 55// Import the i18n module. 56import { i18n } from '@kit.LocalizationKit'; 57 58// Format the phone number. 59let phoneNumberFormat1 = new i18n.PhoneNumberFormat('CN'); 60let formattedPhoneNumber1 = phoneNumberFormat1.format('158****2312'); // formattedPhoneNumber1: 158 **** 2312 61 62// Set the format type of the phone number to RFC3966. 63let phoneNumberFormat2 = new i18n.PhoneNumberFormat('CN', {type: 'RFC3966'}); 64let formattedPhoneNumber2 = phoneNumberFormat2.format('158****2312'); // formattedPhoneNumber2: tel:+86-158-****-2312 65 66// Check whether the phone number is valid. 67let phoneNumberFormat3 = new i18n.PhoneNumberFormat('CN'); 68let isValid = phoneNumberFormat3.isValidNumber('158****2312'); // isValid: true 69 70// Display the home area of the phone number in the specified language. 71let phoneNumberFormat4 = new i18n.PhoneNumberFormat("CN"); 72let locationName4 = phoneNumberFormat4.getLocationName('158****2312', 'en-GB') // locationName4: XiAn, Shanxi 73 74// Format the phone number being dialed. 75let phoneNumberFmt = new i18n.PhoneNumberFormat('CN', {type: 'TYPING'}); 76let phoneNumber : string = "0755453"; 77let formatResult : string = ""; 78for (let i = 0; i < phoneNumber.length; i++) { 79 formatResult += phoneNumber.charAt(i); 80 formatResult = phoneNumberFmt.format(formatResult); 81} 82console.log(formatResult); // formatResult: 0755 453 83``` 84