Documentation
¶
Overview ¶
Package numfmt renders a spreadsheet cell's value the way the cell shows it: through its number format.
A number format is a small language. "#,##0.00" writes 1234.5 as "1,234.50"; "0.0%" writes 0.125 as "12.5%"; up to four sections separated by semicolons give different renderings to positive numbers, negative numbers, zero and text. This package parses that language and applies it.
Two things are deliberately not as displayed:
- Dates and times are written in ISO 8601 whatever the format, because "03/04/2026" names two different days depending on where the file was made. Nothing the value holds is dropped: a value with a time of day keeps it.
- Padding is not reproduced. A format can ask for blank space the width of a digit or of some character, and for a character repeated to fill the cell. Such space is written as one ordinary space, and space at the ends is removed.
The behaviour was fixed against what LibreOffice displays for a table of formats and values (testdata/libreoffice.tsv), and follows it where spreadsheet programs differ.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendGeneral ¶
AppendGeneral appends v in the General format: all of its digits, up to the fifteen a spreadsheet keeps, and scientific notation only where the plain form would be very long.
Types ¶
type Cache ¶ added in v0.1.8
type Cache struct {
// contains filtered or unexported fields
}
Cache remembers what Format.Number made of recent values, so that a value shown again through the same format is copied rather than worked out. Spreadsheets repeat themselves: the same amount, date or code down a column. A remembered text is the text Number gives, since Number depends on the format, the value and the date system only, and all three are the key.
A Cache is for one reader at a time; it is not safe for concurrent use. A reader that reads several sheets at once holds one for each. Reset it when a sheet begins: a sheet's values rarely recur in the next, and a small sheet then costs a small table.
The table starts at 64 entries and doubles, keeping what it holds, each time it has missed as many values as it has entries, up to the limit NewCache is given. Each entry is 24 bytes, plus its text in a buffer bounded at 64 bytes an entry; when the buffer is full the table is emptied. The nil *Cache remembers nothing: its Number is Format.Number. So does a Cache not made by NewCache, the zero value.
A reader that makes a new sheet reader for each sheet can keep its Caches between sheets (in a sync.Pool, say) and Reset them, so that a sheet does not allocate a table of its own.
func NewCache ¶ added in v0.1.8
NewCache returns a Cache of at most entries entries, rounded down to a power of two and to no more than 2^20 (24 MiB), or nil, which remembers nothing, when entries is under one.
type Format ¶
type Format struct {
// contains filtered or unexported fields
}
Format is a parsed number format. It is safe for concurrent use.
func Parse ¶
Parse reads a format code. It does not fail: a code that cannot be understood gives the General format, which shows the value in full.
func (*Format) Number ¶
Number appends v as the format displays it. date1904 says the workbook counts days from 1904.
type Result ¶
type Result uint8
Result says what Number or Text made of a value.
const ( // Shown: the value is displayed. Shown Result = iota // Blank: the format displays nothing for this value. The value is // written in full instead, so that it is not lost; the caller can // report that the cell hides it. Blank // Date: the value is a date or a time, written in ISO 8601. Date )