The values within the table body, specifically those within the body cells, can be formatted with a large selection of fmt_*() methods like fmt_number(), fmt_integer(), fmt_scientific(), and more. Let’s use a portion of the exibble dataset and introduce some formatting to the cell values. First, we’ll generate the basic GT object and take a look at the table without any cell formatting applied.
The num column contains both small and much larger numbers. We can use the fmt_number() method to obtain formatted values have a fixed level of decimal precision and grouping separators. At the same time, we’ll format the numeric values in currency column to get monetary values.
Dates and times can be formatted as well. As long as they are in ISO 8601 form, the fmt_date() and fmt_time() methods can be used to format such values. These methods have corresponding date_style= and time_style= arguments that accept a number of keywords that act as preset formatting styles.
It’s possible to format cells that have already been formatted. Using a formatting method again on previously formatted cells will always work within the ‘last-formatted-wins’ rule.
Within the selected columns= we can choose to target specific cells with the rows= argument. The latter argument allows us to pass in a list of row indices.
Arguments Common to Several Formatting Methods/Functions
While we can use the fmt_*() methods on a table, we can also use the functional versions of these methods on scalar values or lists of values. These variants exist within the vals module. While arguments across these functions and their corresponding method aren’t exactly the same, there are nonetheless many arguments that are shared amongst them. Here are a few examples:
decimals=: set a fixed precision of decimal places
sep_mark=, dec_mark=: set digit separators and the decimal symbol (defaults are "," and ".")
scale_by=: we can choose to scale targeted values by a multiplier value
compact=: larger figures (thousands, millions, etc.) can be autoscaled and decorated with the appropriate suffixes (e.g., "10000" becomes "10K")
pattern=: option to use a text pattern for decoration of the formatted values
locale=: providing a locale ID (e.g., "en", "fr", "de-AT", etc.) will result in numeric formatting specific to the chosen locale
Sometimes it’s easier and more convenient to experiment with formatting using the formatting functions in the vals module. There are many options to explore with each type of formatting and so visiting the API Reference is certainly worthwhile.