numfmt

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package numfmt renders numbers the way spreadsheets show them: number format patterns as in Sheets' custom number formats and TEXT(), General's shortest form, rounding on the 15 significant digits users see, and the serial day numbers dates and times are stored as. It knows nothing of cells or sheets.

Index

Constants

This section is empty.

Variables

View Source
var (
	MonthNames = [...]string{"January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December"}
	DayNames = [...]string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
)

Functions

func Accounting

func Accounting(v float64, dec, width int) (string, bool)

Accounting lays out Sheets' Accounting format with dec decimals in width columns: the $ at the left, the number right-aligned with room for a closing parenthesis, negatives in parentheses and zero as a dash. It reports false when the number doesn't fit.

func AccountingIn added in v0.3.0

func AccountingIn(v float64, dec, width int, loc *locale.Locale) (string, bool)

AccountingIn is Accounting as shown in loc: its separators, and its currency symbol at the left of the cell, or after the number where loc writes the symbol after it ("1.234,56 € ").

func AddMonths

func AddMonths(y, m, n int) (int, int)

AddMonths adds n months to month m of year y, returning a month from 1 to 12 and the year it falls in. m itself may be out of range.

func AdjustDecimals

func AdjustDecimals(pat string, delta int) string

AdjustDecimals adds (delta > 0) or removes zeros after the decimal point of every section of a number pattern, as Sheets' Increase and Decrease decimal places do. Date and time patterns are returned unchanged.

func Civil

func Civil(serial int64) (y, m, d int)

Civil converts a serial day number to year, month and day.

func DateSerial

func DateSerial(y, m, d int) float64

DateSerial returns the serial of a date; month and day overflow into the next month or year, as DATE() allows.

func DaysIn

func DaysIn(y, m int) int

DaysIn is the number of days in month m of year y.

func Entered added in v0.4.0

func Entered(s string) string

Entered keeps the first 15 significant digits of a number written in decimal (digits, a point, an exponent) and makes the digits after them zeros, as Sheets and Excel do with a number typed into a formula: =123456789012345678 is 123456789012345000. Text with no more than 15 digits comes back as it is. Numbers typed into cells keep every digit, as files and imports write them with 17 to round-trip.

func Fixed

func Fixed(x float64, n int, mode Rounding) (whole, frac string)

Fixed renders |x| rounded to n decimal places (n may be negative) as its integer digits (no leading zeros, "" for zero) and exactly n decimal digits. It works on the 15 significant digits spreadsheets show, so 1.005 rounds to 1.01 as users expect rather than to the 1.00 its binary value would give.

func Format

func Format(v float64, pat string) string

Format renders v with a number format pattern, as TEXT() does.

func FormatIn added in v0.3.0

func FormatIn(v float64, pat string, loc *locale.Locale) string

FormatIn renders v with a number format pattern as shown in loc: the pattern is written as in en-US ("#,##0.00") and the number shows loc's decimal and thousands separators ("1.234,50" in de-DE).

func General

func General(v float64) string

General renders a number the way Sheets shows it in General format and in text: up to 15 significant digits, no exponent for ordinary magnitudes.

func GeneralFit

func GeneralFit(v float64, width int) string

GeneralFit renders v in General format within width columns: as many digits as fit, rounding decimals first, then scientific notation, then #s.

func GeneralFitIn added in v0.3.0

func GeneralFitIn(v float64, width int, loc *locale.Locale) string

GeneralFitIn is GeneralFit as shown in loc.

func GeneralIn added in v0.3.0

func GeneralIn(v float64, loc *locale.Locale) string

GeneralIn is General as shown in loc: 1,5 in de-DE.

func Localize added in v0.3.0

func Localize(s string, loc *locale.Locale) string

Localize writes a number in General's form (digits, a point, an exponent, no grouping) with loc's decimal separator.

func Round

func Round(x float64, places int, mode Rounding) float64

Round rounds x to places decimal places (negative places round to tens, hundreds...) with mode, on x's 15 significant digits.

func SerialOf

func SerialOf(t time.Time) float64

SerialOf converts a time to a serial in its own time zone.

func Size added in v0.3.0

func Size(v float64, dec int) string

Size shows v bytes as nushell shows a file size: whole bytes below 1000 ("512 B"), and above that dec decimals of the largest unit, a thousand times apart, that keeps the number under 1000. Like nushell, it cuts the digits past dec rather than rounding them, so 2998 bytes is "2.9 kB", never a unit it hasn't reached. Negative sizes show a minus sign.

func SizeIn added in v0.3.0

func SizeIn(v float64, dec int, loc *locale.Locale) string

SizeIn is Size as shown in loc: 1,6 kB in de-DE.

func SplitSerial

func SplitSerial(v float64) (days int64, secs int)

SplitSerial returns the date and time parts of a serial, rounded to the second.

func TimeSerial

func TimeSerial(h, m int, s float64) float64

TimeSerial is the fraction of a day for a time of day.

func Weekday

func Weekday(serial int64) int

Weekday returns 0 for Sunday through 6 for Saturday. Day 0 was a Saturday.

Types

type Rounding

type Rounding int

Rounding is how Fixed and Round treat the digits they drop.

const (
	HalfUp Rounding = iota // ROUND and number formats
	Up                     // away from zero: ROUNDUP
	Down                   // toward zero: ROUNDDOWN
)

Jump to

Keyboard shortcuts

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