What is the purpose of the TEXT function in Excel
The TEXT function in Excel is used to convert a numeric value (like a date, time, or number) into a text string with a specific format. This is particularly useful when you want to display numbers in a particular way or combine numbers, dates, and text in a single cell, but maintain control over the formatting.
Syntax
excel
Copy code
TEXT(value, format_text)
value: The numeric value (like a number, date, or time) that you want to format.format_text: The format in which you want to display thevalue. This is a text string that specifies the desired format.
Why Use the TEXT Function?
- Custom Formatting: Display numbers, dates, and times in a custom format.
- Concatenation with Text: Combine formatted numbers or dates with text.
- Control Over Display: Keep precise control over how data is displayed, regardless of the cell’s underlying format.
Common Uses of the TEXT Function
1. Formatting Numbers
You can use the TEXT function to apply custom number formats such as adding commas, controlling decimal places, or formatting as currency.
- Commas and Decimal Places
To format the number in A1 with commas and two decimal places:
excel
Copy code
=TEXT(A1, "#,##0.00")
If A1 contains 1234567.89, the result will be 1,234,567.89.
- Currency
To format a number as currency, use:
excel
Copy code
=TEXT(A1, "$#,##0.00")
If A1 contains 1234.5, the result will be $1,234.50.
2. Formatting Dates
The TEXT function is commonly used to format dates in a specific way.
- Full Date Format
To format a date in A1 as January 21, 2024, use:
excel
Copy code
=TEXT(A1, "MMMM DD, YYYY")
If A1 contains 21-Jan-2024, the result will be January 21, 2024.
- Day/Month/Year
To display a date as 21/01/2024, use:
excel
Copy code
=TEXT(A1, "DD/MM/YYYY")
- Month and Year Only
To display only the month and year, you can use:
excel
Copy code
=TEXT(A1, "MMMM YYYY")
If A1 contains 21-Jan-2024, the result will be January 2024.
3. Formatting Time
The TEXT function can also be used to format time.
- Hours and Minutes
To display the time in A1 as 2:30 PM, use:
excel
Copy code
=TEXT(A1, "h:mm AM/PM")
- 24-Hour Format
To display the time in 24-hour format as 14:30, use:
excel
Copy code
=TEXT(A1, "HH:MM")
4. Combining Text and Formatted Numbers/Dates
The TEXT function is useful for combining formatted numbers or dates with text in the same cell.
- Example 1: Combine Date with Text
Suppose you want to combine a date in A1 with text:
excel
Copy code
="The report was generated on " & TEXT(A1, "MMMM DD, YYYY")
If A1 contains 21-Jan-2024, the result will be The report was generated on January 21, 2024.
- Example 2: Combine Number with Text
If you want to combine a number in B1 with text:
excel
Copy code
="The total sales are " & TEXT(B1, "$#,##0.00")
If B1 contains 1234.5, the result will be The total sales are $1,234.50.
Common Format Codes for TEXT
Here’s a quick reference for some common formatting codes:
-
Numbers
0: Displays insignificant zeros (e.g.,TEXT(7, "000")→007)#: Displays numbers but skips insignificant zeros (e.g.,TEXT(7, "###")→7)#,##0: Adds comma separators (e.g.,TEXT(1234567, "#,##0")→1,234,567)#,##0.00: Displays two decimal places (e.g.,TEXT(1234.56, "#,##0.00")→1,234.56)
-
Dates
DD: Day (e.g.,01to31)DDD: Abbreviated day name (e.g.,Mon,Tue)DDDD: Full day name (e.g.,Monday,Tuesday)MM: Month number (e.g.,01for January)MMM: Abbreviated month name (e.g.,Jan,Feb)MMMM: Full month name (e.g.,January,February)YYYY: Full year (e.g.,2024)
-
Time
h: Hours without leading zero (e.g.,1)hh: Hours with leading zero (e.g.,01)mm: Minutesss: SecondsAM/PM: Displays time in 12-hour format with AM or PM
Important Notes
- The Result is Text: The output of the
TEXTfunction is always a text string, even if it looks like a number or date. - Cannot Be Used for Calculations: Since the result is text, you cannot use the output of
TEXTin mathematical calculations. If you need to perform calculations, use the original value instead of the formatted text version.
Summary
The TEXT function is powerful for controlling the appearance of numbers, dates, and times. It allows you to:
- Format numbers with currency, decimals, or thousands separators.
- Format dates and times in a variety of custom formats.
- Combine numbers, dates, and times with text in a single cell.
This function gives you precise control over how your data is displayed, making it especially useful for reports, dashboards, and presentations.