...
Tawesoft Logo

Package lxstrconv

import "tawesoft.co.uk/go/lxstrconv"
Overview
Index

Overview ▾

Package lxstrconv is an attempt at implementing locale-aware parsing of numbers that integrates with golang.org/x/text.

If golang.org/x/text is ever promoted to core then there will be a new version of this package named `lstrconv` (dropping the 'x').

Package Stability

THIS IS A PREVIEW RELEASE, SUBJECT TO BREAKING CHANGES.

Todo:

* checks for integer overflow

* different representations of negative numbers e.g. `(123)` vs `-123`

* In cases where AcceptInteger/AcceptFloat reach a syntax error, they currently underestimate how many bytes they successfully parsed when the byte length of the string is not equal to the number of Unicode code points in the string.

Example

This example demonstrates British, Dutch, and Arabic locale number parsing.

package main

import (
    "fmt"
    "golang.org/x/text/language"
    "tawesoft.co.uk/go/lxstrconv"
)

func checked(f float64, e error) float64 {
    if e != nil {
        panic(e)
    }
    return f
}

func main() {
    dutch   := lxstrconv.NewDecimalFormat(language.Dutch)
    british := lxstrconv.NewDecimalFormat(language.BritishEnglish)
    arabic  := lxstrconv.NewDecimalFormat(language.Arabic)

    fmt.Printf("%f\n", checked(british.ParseFloat("1,234.56")))
    fmt.Printf("%f\n", checked(dutch.ParseFloat("1.234,56")))
    fmt.Printf("%f\n", checked(arabic.ParseFloat("١٬٢٣٤٫٥٦")))
}

Example

You can give end-users examples of the input you expect for a given locale using the /x/text package:

package main

import (
    "golang.org/x/text/language"
    "golang.org/x/text/message"
    "golang.org/x/text/number"
)

func main() {

    message.NewPrinter(language.English).Println(number.Decimal(123456789))
    // Prints 123,456,789

    message.NewPrinter(language.Dutch).Println(number.Decimal(123456789))
    // Prints 123.456.789

    message.NewPrinter(language.Malayalam).Println(number.Decimal(123456789))
    // Prints 12,34,56,789

    message.NewPrinter(language.Bengali).Println(number.Decimal(123456789))
    // Prints ১২,৩৪,৫৬,৭৮৯
}

Package Information

License: MIT (see LICENSE.txt)

Stable: no

For more information, documentation, source code, examples, support, links, etc. please see https://www.tawesoft.co.uk/go and https://www.tawesoft.co.uk/go/lxstrconv

type NumberFormat

NumberFormat defines an interface for parsing numbers in a specific format (such as a decimal number in a specific locale, with support for a digit separator such as commas and a decimal point). Numbers are assumed to be in the normal base (e.g. base 10 for decimal) for that locale.

Errors are either nil, strconv.ErrSyntax or strconv.ErrRange

type NumberFormat interface {
    ParseInt(string) (int64, error)
    ParseFloat(string) (float64, error)

    // AcceptInt parses as much of an integer as possible. The second return
    // value is the number of bytes (not runes) successfully parsed. The error
    // value is always either nil or strconv.ErrRange.
    AcceptInt(string) (int64, int, error)

    // AcceptFloat parses as much of a float as possible. The second return
    // value is the number of bytes (not runes) successfully parsed. The error
    // value is always either nil or strconv.ErrRange.
    AcceptFloat(string) (float64, int, error)
}

func NewDecimalFormat

func NewDecimalFormat(tag language.Tag) NumberFormat

NewDecimalFormat constructs, for a given locale, a NumberFormat that defines how a decimal (base-10) number should be parsed. Note that the behaviour is undefined for locales that have non-base-10 number systems.