numfmt

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func AppendGeneral(dst []byte, v float64) []byte

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.

func Builtin

func Builtin(id int) string

Builtin returns the code of a built-in format, by its number. The numbers Excel reserves for East Asian dates (27 to 36, 50 to 58) are given as a date. Unknown numbers are General.

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

func NewCache(entries int) *Cache

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.

func (*Cache) Number added in v0.1.8

func (c *Cache) Number(f *Format, dst []byte, v float64, date1904 bool) ([]byte, Result)

Number appends v as f displays it, as f.Number does, remembering the text for the next time v is shown through f.

func (*Cache) Reset added in v0.1.8

func (c *Cache) Reset()

Reset empties c and returns it to its first size, keeping its memory. The whole table is emptied, so that it holds no Format of the sheets before.

type Format

type Format struct {
	// contains filtered or unexported fields
}

Format is a parsed number format. It is safe for concurrent use.

func General

func General() *Format

General returns the General format.

func Parse

func Parse(code string) *Format

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) IsDate

func (f *Format) IsDate() bool

IsDate reports whether the format shows numbers as dates or times.

func (*Format) Number

func (f *Format) Number(dst []byte, v float64, date1904 bool) ([]byte, Result)

Number appends v as the format displays it. date1904 says the workbook counts days from 1904.

func (*Format) PlainText

func (f *Format) PlainText() bool

PlainText reports whether the format shows a cell's text as it is, which nearly every format does.

func (*Format) Text

func (f *Format) Text(dst []byte, text string) ([]byte, Result)

Text appends a cell's text as the format displays it.

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
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL