Formula - What to try when numbers don't format correctly

A frustrating problem that app builders sometimes encounter, is that the formula they use to format numbers does not work.

The typical scenario looks like this. An app builder attempts to format a number using the text function. The usage of this function looks like this:

Formula                         Output
---------------------------------------------
Text(60934.2, "#,#.000") 60,934.200
Text(60934.2, "#,#.###") 60,934.2
Text(22934.23624, "#.00") 22934.24

When the app builder applies this formula to the text property of a label control, the output remains unformatted.The screenshot beneath illustrates this problem. Notice how we attempt to format the 'acquisition price' value with thousand comma separators and to hide decimal places, yet the text in the label does not display the desired format.


Typical cause of the problem

In almost all cases, the cause of the problem is that Power Apps doesn't recognise the underlying field as a number. There can be for a number of reasons for this.

  • If we attempt to call the text function on a SharePoint list or database column that is of data type text, the Text function will fail to apply numeric formatting.
  • If we use an Excel spreadsheet, Power Apps may not recognise the column as numeric.
  • With SharePoint, Power Apps does not correctly recognise calculated columns as numbers. My post here provides more details on how to fix this problem.
    http://powerappsguide.com/blog/post/sharepoint-beware-of-calculated-columns

We can confirm whether Power Apps correctly recognises a field as a number by examining the icon that appears next to the field in the designer. A numeric field displays a "123" icon, as opposed to a "abc" icon. 

How to fix the problem

If the data type of the underlying field is text, the preferred fix is to change the data type to a numeric data type.

To set up an Excel spreadsheet so that Power Apps recognises it as numeric, it's important to confirm that none of the cells in the column contain non-numeric text characters (including leading/trailing spaces). We can then select the column, and use the data type drop-down in the ribbon bar to set the data type to 'Number'.

When we add an Excel data source to Power Apps, it's important that the spreadsheet contains some rows with numbers. If we connect an empty spreadsheet to Power Apps, it has no way to determine that a column is numeric when there are no rows.


After we modify the data type of an existing data source, we must refresh the data source from Power Apps through the data panel.


In cases where we are unable to change the data type of the data column, alternative is to convert the text value to a number, before formatting it with the text function. Here's an example of how this formula looks:

Text(Value(Parent.Default), "#,#")
Taking the example that we used earlier, we now notice that this formula properly formats the number with 1000 comma separators.


An alternative formula would look like this:
If(IsNumeric(Parent.Default),
Text(Value(Parent.Default), "#,#"),
Parent.Default
)
This formula applies an additional test. If Power Apps cannot convert the text value to a number (due to the existence of non-numeric characters), it falls back to displaying the unformatted value, rather than an empty string.

Conclusion

A frustrating problem that app builders sometimes encounter is the inability to correctly format numbers. The most common cause of this problem is that the data type of the source value is text. We can fix this issue by changing the data type of the source column, or by calling the Value function to convert the text value to a number.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021