The Format function in Microsoft Dynamics 365 Business Central converts a value (number, date, time, Boolean, or enum) into a text string according to a specified format. The basic syntax is Format(Value [, Length] [, FormatString]), and the easiest way to use it is Format(1234.5, 0, ' which returns “1,234.50”. This article explains the syntax, common format strings, examples, and pitfalls.
What Does the Format Function Do?
In AL, every data type has a default text representation. The Format function lets you control that representation. For example, a Date value like 20250115D displays as “01/15/2025” by default, but you can format it as “January 15, 2025” or “2025-01-15”. Similarly, a Decimal like 1234.5 can appear with thousands separators, fixed decimals, or as a percentage.
The function returns a Text value, so you can assign it to a text variable or use it directly in a message or a field. It is available in AL for both table fields and local variables. The syntax is:
Format(Value, Length, FormatString)
- Value – the expression to format (any data type).
- Length – optional integer. If you specify a length, the output is padded with spaces (or truncated) to that length. If 0, no padding.
- FormatString – optional string that defines the format. If omitted, the default format for the data type is used.
How to Use Format Strings: The Syntax
The FormatString parameter uses a special syntax with placeholders inside angle brackets. The most common placeholders are:
<Precision,N:M>– controls decimal places. N is the minimum number of decimals, M is the maximum. For example,<Precision,2:2>forces exactly two decimals.<Standard Format,0>– uses the standard format for the data type (e.g., thousand separators for numbers).<Integer>– displays the integer part only.<Currency>– adds the currency symbol from the system.<Date>,<Time>,<DateTime>– for date/time values, you can specify a custom format like<Date,dd-MM-yyyy>.
For dates and times, you can also use a format string like <Date,dd/MM/yyyy> or <Time,HH:mm:ss>. The codes are similar to .NET format strings but with angle brackets.
Examples: Formatting Numbers, Dates, and More
Let’s see concrete examples you can test in AL.
Number with two decimals and thousand separator:
Format(1234.5, 0, '<Precision,2:2><Standard Format,0>') // returns "1,234.50"
Number without decimals:
Format(1234.5, 0, '<Precision,0:0>') // returns "1235" (rounds)
Date as DD/MM/YYYY:
Format(20250115D, 0, '<Date,dd/MM/yyyy>') // returns "15/01/2025"
Time as HH:MM:
Format(133000T, 0, '<Time,HH:mm>') // returns "13:30"
Boolean as Yes/No:
Format(true, 0, '<Yes/No>') // returns "Yes"
Note: The <Yes/No> format is not standard; you might need to use an IF statement. Actually, the Format function does not have a built-in Yes/No format. For Boolean, use Format(true) which returns “Yes” or “No” depending on the language. So Format(true) gives “Yes” in English.
Choosing the Right Format String
The key is to know what you want the output to look like. For numbers, decide on the number of decimal places and whether to use thousands separators. For dates, decide the order of day, month, year, and whether to use leading zeros. For times, decide 24-hour vs 12-hour format.
Here are some common format strings:
<Precision,2:2>– always two decimals.<Precision,0:2>– up to two decimals, but no trailing zeros.<Standard Format,0>– uses the regional settings for the user (e.g., comma as decimal separator in some countries).<Date,dd-MM-yyyy>– day-month-year with dashes.<Date,yyyy-MM-dd>– ISO format.<Time,HH:mm:ss>– 24-hour time with seconds.
If you omit the format string, the function uses the default format for the data type, which is often the user’s regional settings.
Common Problems and Misconceptions
Problem: Format returns a comma instead of a period for decimals. This happens because the format string uses the user’s regional settings. If you need a fixed decimal separator, you can use a custom format like <Precision,2:2> but the separator still follows the locale. To force a period, you might need to replace the comma manually: Format(1234.5, 0, '<Precision,2:2>') might return “1.234,50” in German. You can do Format(1234.5, 0, '<Precision,2:2>').Replace(',', '.') but that also replaces thousands separators.
Misconception: The Length parameter truncates the number. Actually, it truncates the entire string to the specified length, which can cut off digits. Use it only for fixed-width output.
Problem: Formatting a DateTime with a Date format string returns only the date part. That’s correct – if you use <Date,...> on a DateTime, it ignores the time. Use <DateTime,...> to include both.
Problem: The format string is case-sensitive. Yes, <date> is not the same as <Date>. Always use the exact case from the documentation.
Format vs. FormatString Property on Fields
In table and page fields, you can set a Format property directly in the AL code, which applies automatically when the field is displayed. For example:
field(10; "My Decimal"; Decimal) { Format = '<Precision,2:2>'; }
This is different from the Format function. The property is static and applies to all values of that field. The function is dynamic and can be used in code to format a value on the fly.
Alternatives to Format
If you need more complex formatting, you can use string concatenation and functions like CopyStr, DelChr, or Text functions. For example, to pad a number with leading zeros:
Format(5, 0, '<Precision,0:0>') // returns "5"
// To get "005":
CopyStr('000', 1, 3 - StrLen(Format(5))) + Format(5)
For dates, you can also use Date2DMY or Date2DWY functions to extract parts, but Format is simpler.
Frequently Asked Questions
Q: How do I format a number with a leading zero? Use Format(5, 0, '<Precision,0:0>') gives “5”. To get “05”, use Format(5, 2, '<Precision,0:0>') – the Length parameter pads with spaces, not zeros. To pad with zeros, use CopyStr as shown above.
Q: Can I use Format to convert a decimal to a string without rounding? No, the function always rounds to the specified precision. If you need to truncate, use Truncate or Round first.
Q: Why does Format return an empty string for a null value? If the value is null (e.g., a blank record field), Format returns an empty string. That’s the expected behavior.
Q: Does Format work on enums? Yes, it returns the enum’s caption (the label). For example, Format(Enum::Status) returns the caption of the enum value.
Q: How do I format a date as “Month Day, Year”? Use Format(20250115D, 0, '<Date,MMMM dd, yyyy>') – but note that month names depend on the user’s language.
Q: Is there a way to format without using the Format function? Yes, you can use string concatenation with Format or use the Text data type with StrSubstNo for simple replacements, but Format is the standard.
Related
- Clear Formatting in Excel: What It Does and How to Use It
- Shortcut for Refresh: F5, Ctrl+R, and More
- Maximum Number of Entries Has Been Reached? Explained
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Row and Column AutoFit: Steps, Limits, and FixesTo auto-adjust Excel row or column size, select the relevant rows or columns, then choose Home > Cells…
- Python join() Explained: Syntax, Examples, and TipsPython’s str.join() method combines strings from an iterable into one string, placing the string before the dot between…
- 0 in Excel Conditional Formatting: 10 Practical RulesTo highlight zero values in Excel for Microsoft 365 on Windows, select the cells, then choose Home >…
- Consolidate Data in Excel Across Multiple WorksheetsYes. In Excel for Microsoft 365 on Windows, use Data > Consolidate to summarize matching ranges from multiple…
- US Trade Balance by Month: Data & TrendsThe U.S. trade balance by month is the difference between the value of goods and services the United…
- How to Delete a Page in Word Using a Keyboard ShortcutTo delete a page in Word on Windows, press Ctrl+G, type \page in the page-number box, press Enter,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.