How Do I Format a Number in a Column?
This Lesson presents how to format a number in a Data Grid column, using the Data Grid Helper Plugin (DGH).
First, drag a data grid onto a stack and double click on it to open the Data Grid Helper (DGH) Properties Palette.
Open the Scripts Topics and install the Number Formatting script.
Click the (+) button and add three columns.
Name the columns:
- Sales Amount
- Phone Numbers
Open the Behaviors topic (1)
Double click on the "Sales Amount" header (2) to open the Column Content Area
Next, drag the Custom Script from the Column Builder Properties and drop it in the Column Content Area (3)
Open the Column Builder's Format topic. (2)
In this column, we would like to have:
- numbers truncated to two decimals
- negative numbers in red, without the negative sign
- a comma as the thousands separator
In the Pattern property, type this pattern:
### ### ##0.00,### ### ##0.00[red]
Note the pattern is possibily divided in three items:
- the first for positive numbers
- the second for negative numbers
- the last for Zero values
For more information about how to build a formatting pattern, please follow this lesson:
In the Thousands Separtor property, type a comma.
DGH can now build the required script for applying a format to the column.
Click the Script Editor icon and select the "Build Script" menu.
Click the "Erase" button to replace the column default script with the script built by DGH.
Before to validate our work on the Sales Amount column, this is better to test how our column will be presented.
In the Column Builder Preview, you can input some amounts in the three preview cells available for a column:
1742.375 in the first line.
-125 in the second line.
3174987.5 in the last line.
By doing that, you have probably noticed the number is formatted when the cell is closed, and that it reappears at its initial input when one edits it again.
That is because the format applied is a displaying format. It change only how the number is represented, and keeps the number unchanged in the data. This leaves the possibility of to continue sorting the column or to apply some calcul to the amounts such as doing sums.
To have the amounts perfectly aligned in the column, we can now changing it's Content Alignment by selecting the "Right" value.