numfmt

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 5 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 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 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 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 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 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