Class Format
- Namespace
- NuvTools.Validation.Brazil
- Assembly
- NuvTools.Validation.dll
Provides formatting utilities for Brazilian document numbers, phone numbers and ZIP codes.
public static class Format
- Inheritance
-
Format
- Inherited Members
Methods
FormatCNPJ(string)
Formats a CNPJ string into the standard Brazilian format: XX.XXX.XXX/XXXX-XX. Accepts both legacy numeric and the new RFB alphanumeric (uppercase A–Z and 0–9) representations. Separators in the input are stripped; lowercase letters are normalized to uppercase.
public static string FormatCNPJ(this string cnpj)
Parameters
cnpjstringThe CNPJ number as a string (14 alphanumeric characters, with or without separators).
Returns
- string
The formatted CNPJ string.
Examples
string formatted = "12345678000195".FormatCNPJ();
// Returns: "12.345.678/0001-95"
string alphanumeric = "12ABC34501DE35".FormatCNPJ();
// Returns: "12.ABC.345/01DE-35"
Exceptions
- ArgumentNullException
Thrown when
cnpjis null.- FormatException
Thrown when the input does not contain exactly 14 alphanumeric characters.
FormatCPF(string)
Formats a CPF string into the standard Brazilian format: XXX.XXX.XXX-XX. Separators in the input are stripped; fewer than 11 digits are padded with leading zeros (a CPF kept as a number loses them).
public static string FormatCPF(this string cpf)
Parameters
cpfstringThe CPF number as a string (up to 11 digits, with or without separators).
Returns
- string
The formatted CPF string.
Examples
string formatted = "12345678901".FormatCPF();
// Returns: "123.456.789-01"
Exceptions
- ArgumentNullException
Thrown when
cpfis null.- FormatException
Thrown when the input has no digits or more than 11.
FormatCPForCNPJ(string)
Formats a CPF or a CNPJ, by length: 14 alphanumeric characters are a CNPJ, anything shorter a CPF.
public static string FormatCPForCNPJ(this string value)
Parameters
valuestringThe CPF or CNPJ, with or without separators.
Returns
- string
The formatted document.
Examples
"12345678901".FormatCPForCNPJ(); // "123.456.789-01"
"12345678000195".FormatCPForCNPJ(); // "12.345.678/0001-95"
Exceptions
- ArgumentNullException
Thrown when
valueis null.- FormatException
Thrown when the value is neither a CPF nor a CNPJ in size.
FormatOrOriginal(string?, Func<string, string>)
Applies a formatter for display, keeping the value as it is when it does not fit the format and returning null for an empty value. For showing stored data that may be incomplete or legacy without failing the page or document that shows it.
public static string? FormatOrOriginal(this string? value, Func<string, string> format)
Parameters
valuestringThe value to format.
formatFunc<string, string>The formatter — FormatCPF(string), FormatPhoneNumber(string), ...
Returns
- string
The formatted value, the original value when it does not fit, or null when empty.
Examples
"11999990000".FormatOrOriginal(Format.FormatPhoneNumber); // "(11) 99999-0000"
"123".FormatOrOriginal(Format.FormatZipCode); // "123"
FormatPhoneNumber(string)
Formats a Brazilian phone number: (XX) XXXXX-XXXX for a mobile (11 digits), (XX) XXXX-XXXX for a
landline (10 digits). With the country code (55 in front, 12 or 13 digits), it is kept as +55.
public static string FormatPhoneNumber(this string phoneNumber)
Parameters
phoneNumberstringThe phone number, with or without separators.
Returns
- string
The formatted phone number.
Examples
"11999990000".FormatPhoneNumber(); // "(11) 99999-0000"
"1133334444".FormatPhoneNumber(); // "(11) 3333-4444"
"+5511999990000".FormatPhoneNumber(); // "+55 (11) 99999-0000"
Exceptions
- ArgumentNullException
Thrown when
phoneNumberis null.- FormatException
Thrown when the number has neither 10 nor 11 digits (plus the optional 55).
FormatZipCode(string)
Formats a Brazilian ZIP code (CEP) as XXXXX-XXX.
public static string FormatZipCode(this string zipCode)
Parameters
zipCodestringThe ZIP code, with or without the hyphen.
Returns
- string
The formatted ZIP code.
Examples
"01310100".FormatZipCode(); // "01310-100"
Exceptions
- ArgumentNullException
Thrown when
zipCodeis null.- FormatException
Thrown when the input does not contain exactly 8 digits.