scalingSuffix

package module
v0.0.0-...-2dbe75e Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

enables formatting of numbers, scaled and suffixed, for more compact and readable numerical i/o.

this is done by wrapping a number in a generic type implementing fmt.Scanner+fmt.Stringer, then using that inplace of the number for i/o.

scaling is achieved though manipulation of the string returned by fmt.Print, on the embedded value, not by calcuation.

Index

Examples

Constants

View Source
const (
	SpeedOfLight      = 299792458       // c
	PlanksContant     = 6.62607015e-34  //h {1,2,-1}
	BoltzmannConstant = 1.380649e-23    // b J⋅K−1
	Avagadros         = 6.02214076e23   // NA mol−1
	ElementaryCharge  = 1.602176634e-19 // e
)

Variables

View Source
var (
	Default    = byteReplace{'_', ' '}
	English    = byteReplace{'_', ','}
	NonEnglish = []byteReplace{{'.', ','}, {'_', '.'}}
)

standard byte replacment(s)

View Source
var (
	ToEnglish      = ReplaceString(English)
	FromEnglish    = ReplaceString(reverse(English)...)
	ToNonEnglish   = ReplaceString(NonEnglish...)
	FromNonEnglish = ReplaceString(reverse(NonEnglish...)...)
)

standard string modifiers

View Source
var (
	Distance = Derived(sSI{Length: 1})
	Duration = Derived(sSI{Mass: 1})
	//	Frequency =	Duration.Reciprocal()
	Current = Derived(sSI{Ampere: 1})
)
View Source
var SISuffices = map[int]rune{
	-27: 'q',
	-24: 'y',
	-21: 'z',
	-18: 'a',
	-15: 'f',
	-12: 'p',
	-9:  'n',
	-6:  'µ',
	-3:  'm',
	-2:  'c',
	-1:  'd',
	2:   'h',
	3:   'k',
	6:   'M',
	9:   'G',
	12:  'T',
	15:  'P',
	18:  'E',
	21:  'Z',
	24:  'Y',
	27:  'R',
	30:  'Q',
}
View Source
var SufficesSI = Pivot(SISuffices)
View Source
var SuperReplacer = strings.NewReplacer(
	"-", "⁻",
	"1", "¹",
	"2", "²",
	"3", "³",
	"4", "⁴",
	"5", "⁵",
	"6", "⁶",
	"7", "⁷",
	"8", "⁸",
)

Functions

func CutPrefixN

func CutPrefixN(n int, s string) string

CutPrefixN returns a string without n starting, less if not possible, runes. Note: runes discarded = string[:len(CutPrefixN(n,string))]

func CutSuffixN

func CutSuffixN(n int, s string) string

CutSuffixN returns a string without n trailing, less if not possible, runes. Note: runes discarded = string[len(CutSuffixN(n,string)):]

func DimString

func DimString(d int8, s string) (_ string)

func I18nbyteReplace

func I18nbyteReplace(countryCode [2]byte) func(string) string

I18nbyteReplace determines what byteReplace(s) needed for a language code

func MovePointRight

func MovePointRight(s string, n int) string

move second byte right by n

func NumSep

func NumSep(s string) string

NumSep adds a underscore between every 3 runes starting from the end.

func NumberLimitedReader

func NumberLimitedReader(r io.RuneScanner) io.Reader

NumberLimitedReader returns an io.Reader, Reading from a provided io.RuneScanner, closing it when reaching anything not possible for a number.

Example
io.Copy(os.Stdout, NumberLimitedReader(strings.NewReader("123v")))
fmt.Println()
io.Copy(os.Stdout, NumberLimitedReader(strings.NewReader("123.123v")))
fmt.Println()
io.Copy(os.Stdout, NumberLimitedReader(strings.NewReader("-123v")))
fmt.Println()
io.Copy(os.Stdout, NumberLimitedReader(strings.NewReader("123_123v")))
fmt.Println()
Output:

123
123.123
-123
123_123

func NumberUnderscoreSep

func NumberUnderscoreSep(n string) string

NumberUnderscoreSep adds a underscore between every 3 runes starting from the dp.

Example
fmt.Println(NumberUnderscoreSep("123"))
fmt.Println(NumberUnderscoreSep("1234.5678900"))
fmt.Println(NumberUnderscoreSep("1"))
Output:

123
1_234.5678900
1

func Pivot

func Pivot[Map ~map[K]V, K, V comparable](m Map) map[V]K

Pivot returns a new map, made from the provided maps key/values swapped (values thus need to be comparable)

func ReplaceString

func ReplaceString(bps ...byteReplace) func(string) string

ReplaceString replaces bytes in a string, each replace is a single byte, so no change in length. so for the string single byte chars, ASCII, only.

func Scale

func Scale(s string) string

func SepEvery

func SepEvery(n int, s, sep string) string

SepEvery adds a string between every n runes starting from the end.

Example
fmt.Println(SepEvery(1, "123", "_"))
fmt.Println(SepEvery(3, "123456789", "_"))
fmt.Println(SepEvery(3, "1", "_"))
Output:

1_2_3
123_456_789
1

func ShiftDP

func ShiftDP(bs []byte, n int) []byte
Example
fmt.Println(string(ShiftDP([]byte("123123"), 2)))
fmt.Println(string(ShiftDP([]byte("123.123"), 2)))
fmt.Println(string(ShiftDP([]byte("123.123"), 4)))
fmt.Println(string(ShiftDP([]byte("123.123"), -2)))
fmt.Println(string(ShiftDP([]byte("123.123"), -3)))
fmt.Println(string(ShiftDP([]byte("123123"), -2)))
fmt.Println(string(ShiftDP([]byte("123123"), -8)))
fmt.Println(string(ShiftDP([]byte("123.123"), -8)))
fmt.Println(string(ShiftDP([]byte("-123123"), 2)))
fmt.Println(string(ShiftDP([]byte("-123.123"), 2)))
fmt.Println(string(ShiftDP([]byte("-123.123"), 4)))
fmt.Println(string(ShiftDP([]byte("-123.123"), -2)))
fmt.Println(string(ShiftDP([]byte("-123.123"), -3)))
fmt.Println(string(ShiftDP([]byte("-123123"), -2)))
fmt.Println(string(ShiftDP([]byte("-123123"), -8)))
fmt.Println(string(ShiftDP([]byte("-123.123"), -8)))
Output:

12312300
12312.3
1231230
1.23123
0.123123
1231.23
0.00123123
0.00000123123
-12312300
-12312.3
-1231230
-1.23123
-0.123123
-1231.23
-0.00123123
-0.00000123123

func ShiftDPLeft

func ShiftDPLeft(bs []byte, n int) []byte

shifts the decimal point

func ShiftDPRight

func ShiftDPRight(bs []byte, n int) []byte

shifts the decimal point

func ShiftPosDP

func ShiftPosDP(bs []byte, n int) []byte

func SmallerSuffix

func SmallerSuffix(e int) (int, rune)

SmallerSuffix searches for the next smaller suffix.

func Tags

func Tags(t reflect.Type, l string) iter.Seq[string]

func Thousands

func Thousands(s string) string

apply NumSep to digits above dp

func ToSystembyteReplace

func ToSystembyteReplace(countryCode byteReplace) func(string) string

ToSystembyteReplace determines what byteReplace is needed for the system language

func TrimNumber

func TrimNumber(s string) string

TrimNumber removes trailing redundent, for scanning as a number, runes.

func UntilReader

func UntilReader(r io.RuneScanner, fn func(rune) bool) io.Reader

UntilReader returns an io.Reader, Reading from a provided io.RuneScanner, closing it when a read rune causes the provided function to return true.

Types

type CGS

type CGS struct {
	Mass    int8 `unit:"g" dim:"m" SI:"0.001"`
	Length  int8 `unit:"cm" dim:"L" SI:"0.01"`
	Time    int8 `unit:"s" dim:'t'`
	Ampere  int8 `unit:"A" dim:'I'`
	Kelvin  int8 `unit:"K" dim:"θ"`
	Mole    int8 `unit:"mol" dim:"N"`
	Candela int8 `unit:"Cd" dim:"J"`
}

type Constant

type Constant float64

type Derived

type Derived sSI

type Imperial

type Imperial struct {
	Mass   int8 `unit:"lb" dim:"M" SI:"0.45359237"`
	Length int8 `unit:"ft" dim:"L" SI:"0.3048"`
	Time   int8 `unit:"s" dim:"T"`
}

type Modified

type Modified struct {
	Derived
	Constant
}

type Number

type Number interface {
	constraints.Float | constraints.Integer
}

type SI

type SI[N Number] struct {
	// contains filtered or unexported fields
}

SI's are fmt.Stringer and fmt.Scanner where the i/o is scaled, from the embedded value, using powers of 10 and with the metric prefix appended.

func NewSI

func NewSI[N Number](v N) SI[N]
Example
fmt.Println(NewSI(1_234_500))
fmt.Println(NewSI(-12345000))
fmt.Println(NewSI(int64(123_450_000_000_000)))
Output:

1.2345M
-12.345M
123.45T

func (*SI[N]) Scan

func (s *SI[N]) Scan(state fmt.ScanState, verb rune) (err error)

scans from any SI suffixed number, not just SI thousands.

Example
var i SI[int64]
fmt.Sscan("1h", &i)
fmt.Println(i)
fmt.Sscan("1k", &i)
fmt.Println(i)
fmt.Sscan("123450M", &i)
fmt.Println(i)
fmt.Sscan("-1000", &i)
fmt.Println(i)
fmt.Sscan("-1000000000000000", &i)
fmt.Println(i)
Output:

100
1k
123.45G
-1k
-1P

func (SI[N]) String

func (i SI[N]) String() string

returns the value with the maximum number of 1000's removed and replaced with the appropriate suffix added. note: doesn't use small, less than 1000, value SI suffixes.

type SIUnits

type SIUnits[N Number] struct {
	SI[N]
	fmt.Stringer
}

SIUnits's are SI's with metric units suffixes added.

func NewSIUnits

func NewSIUnits[N Number](v N, f fmt.Stringer) SIUnits[N]

Jump to

Keyboard shortcuts

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