Documentation
¶
Overview ¶
decstr is a package for detecting and converting decimal strings. It provides utilities for identifying decimal formats and converting between them.
Example ¶
Example demonstrates general usage of the decstr package, including normalization, format detection, and conversion of decimal strings.
decimal := "1'234'567,89" // Normalize example normalized := Normalize(decimal) fmt.Println("Normalized:", normalized) // Detect format example format, ok := DetectFormat(decimal) fmt.Println("Detected format:", format, "ok:", ok) // Convert example df := DecimalFormat{Point: '.', Group: ' ', Standard: false} converted, ok := df.Convert(decimal) fmt.Println("Converted:", converted, "ok:", ok)
Output: Normalized: 1234567.89 Detected format: {`,`, `'`, standard} ok: true Converted: 12 34 567.89 ok: true
Index ¶
Examples ¶
Constants ¶
const NoSeparator = rune(0)
NoSeparator represents the absence of a separator and is the 0 rune.
Variables ¶
This section is empty.
Functions ¶
func IsNormalized ¶
func IsNormalized[T bytestr](decimal T) bool
IsNormalized checks if a decimal string is normalized. A normalized decimal string adheres to the following rules:
- May start with a '-' (negative sign).
- Must be followed by one or more digits.
- If a '.' is present, it must be followed by one or more digits.
- Cannot start with '0' unless the integer part is exactly 0.
- Cannot have trailing zeros after the '.' (e.g., "123.000" -> false).
- Cannot have a trailing '.' (e.g., "123." -> false).
- The string cannot be empty.
Example ¶
fmt.Println(IsNormalized("-123.45")) fmt.Println(IsNormalized("1 234.5"))
Output: true false
func Normalize ¶
func Normalize[T bytestr](decimal T) (normalized T)
Normalize returns a normalized decimal string. A normalized decimal string adheres to the following rules:
- May start with a '-' (negative sign).
- Is followed by one or more digits.
- If a '.' is present, it is followed by one or more digits (e.g., "123." -> "123").
- Cannot start with '0' unless the integer part is exactly 0 (e.g., "0123.4" -> "123.4").
- Cannot have trailing zeros after the '.' (e.g., "123.000" -> "123").
- Cannot have a trailing '.' (e.g., "123." -> "123").
Example ¶
fmt.Println(Normalize(" - 1 234,50 ")) fmt.Println(Normalize("12 345."))
Output: -1234.5 12345
func NormalizeCheck ¶
func NormalizeCheck[T bytestr](decimal T) (normalized T, ok bool)
NormalizeCheck returns a normalized decimal string and a boolean. The boolean `ok` is true if the input string was successfully normalized; otherwise, it is false, indicating the input string is unchanged.
Types ¶
type DecimalFormat ¶
DecimalFormat describes the format of a decimal string.
- Point: The decimal separator (or NoSeparator if absent).
- Group: The grouping separator (or NoSeparator if absent).
- Standard: True if grouping follows a standard pattern (e.g., groups of 3 digits), False if it uses a non-standard pattern (e.g., 3 digits then 2 digits).
func DetectFormat ¶
func DetectFormat[T bytestr](decimal T) (df DecimalFormat, ok bool)
DetectFormat detects the decimal format of a string. It returns the detected DecimalFormat and a boolean indicating success. The boolean `ok` is false if the string does not contain a valid decimal format or if the format is ambiguous. If it is impossible to determine whether the grouping is standard or non-standard, it defaults to standard.
Example ¶
df, ok := DetectFormat("1 234,56") if !ok { fmt.Println("not a decimal") } fmt.Println(df)
Output: {`,`, ` `, standard}
func (DecimalFormat) Convert ¶
func (df DecimalFormat) Convert(decimal string) (new string, ok bool)
Convert converts a decimal string to a formatted decimal string using the specified DecimalFormat. If the input string is not a valid decimal string, it returns "0" and false. The input string does not need to be a normalized decimal string. The output string is formatted based on the following rules:
- Grouping separators are inserted every 3 or 2 digits (depending on `df.Standard`).
- A custom decimal separator (`df.Point`) is used.
- Negative numbers retain their '-' sign. If + is present, it is removed.
Example ¶
df := DecimalFormat{Point: ',', Group: ' ', Standard: true} new, ok := df.Convert("123456789.123") if !ok { fmt.Println("not a decimal") } fmt.Println(new)
Output: 123 456 789,123
func (DecimalFormat) String ¶
func (df DecimalFormat) String() string
String returns a string representation of the DecimalFormat, formatted as {`<Point>`, `<Group>`, <standard|non-standard>}.