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 ¶
- Constants
- Variables
- func CutPrefixN(n int, s string) string
- func CutSuffixN(n int, s string) string
- func DimString(d int8, s string) (_ string)
- func I18nbyteReplace(countryCode [2]byte) func(string) string
- func MovePointRight(s string, n int) string
- func NumSep(s string) string
- func NumberLimitedReader(r io.RuneScanner) io.Reader
- func NumberUnderscoreSep(n string) string
- func Pivot[Map ~map[K]V, K, V comparable](m Map) map[V]K
- func ReplaceString(bps ...byteReplace) func(string) string
- func Scale(s string) string
- func SepEvery(n int, s, sep string) string
- func ShiftDP(bs []byte, n int) []byte
- func ShiftDPLeft(bs []byte, n int) []byte
- func ShiftDPRight(bs []byte, n int) []byte
- func ShiftPosDP(bs []byte, n int) []byte
- func SmallerSuffix(e int) (int, rune)
- func Tags(t reflect.Type, l string) iter.Seq[string]
- func Thousands(s string) string
- func ToSystembyteReplace(countryCode byteReplace) func(string) string
- func TrimNumber(s string) string
- func UntilReader(r io.RuneScanner, fn func(rune) bool) io.Reader
- type CGS
- type Constant
- type Derived
- type Imperial
- type Modified
- type Number
- type SI
- type SIUnits
Examples ¶
Constants ¶
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 ¶
var ( Default = byteReplace{'_', ' '} English = byteReplace{'_', ','} NonEnglish = []byteReplace{{'.', ','}, {'_', '.'}} )
standard byte replacment(s)
var ( ToEnglish = ReplaceString(English) FromEnglish = ReplaceString(reverse(English)...) ToNonEnglish = ReplaceString(NonEnglish...) FromNonEnglish = ReplaceString(reverse(NonEnglish...)...) )
standard string modifiers
var ( Distance = Derived(sSI{Length: 1}) Duration = Derived(sSI{Mass: 1}) // Frequency = Duration.Reciprocal() Current = Derived(sSI{Ampere: 1}) )
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',
}
var SufficesSI = Pivot(SISuffices)
var SuperReplacer = strings.NewReplacer(
"-", "⁻",
"1", "¹",
"2", "²",
"3", "³",
"4", "⁴",
"5", "⁵",
"6", "⁶",
"7", "⁷",
"8", "⁸",
)
Functions ¶
func CutPrefixN ¶
CutPrefixN returns a string without n starting, less if not possible, runes. Note: runes discarded = string[:len(CutPrefixN(n,string))]
func CutSuffixN ¶
CutSuffixN returns a string without n trailing, less if not possible, runes. Note: runes discarded = string[len(CutSuffixN(n,string)):]
func I18nbyteReplace ¶
I18nbyteReplace determines what byteReplace(s) needed for a language code
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 ¶
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 ¶
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 SepEvery ¶
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 ¶
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 ShiftPosDP ¶
func SmallerSuffix ¶
SmallerSuffix searches for the next smaller suffix.
func ToSystembyteReplace ¶
ToSystembyteReplace determines what byteReplace is needed for the system language
func TrimNumber ¶
TrimNumber removes trailing redundent, for scanning as a number, runes.
func UntilReader ¶
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 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 ¶
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 ¶
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