Skip to content

Commit

Permalink
handles all numeric types, also unsigneds (and byte and rune as they'…
Browse files Browse the repository at this point in the history
…re aliases)
  • Loading branch information
jagottsicher committed Oct 7, 2022
1 parent 237315b commit 8550419
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
myGoToolbox is package which contains a collection of useful functions and handy tools for recurring tasks in your Go projects

## thousands.go
thousands.go mainly consists of the function Separate which adds thousands separators to numbers. It takes two arguments: the number and a language-code you want to use. It returns a string with thousands separators as commonly used in this lang/region. Actually Separate supports English, German and French (intl. recommended) notation. More information about [decimal separators](https://en.wikipedia.org/wiki/Decimal_separator).
thousands.go mainly consists of the function `Separate()` which adds thousands separators to numbers. It takes two arguments: the number and a language-code you want to use. It returns a string with thousands separators as commonly used in this lang/region. Actually `Separate()` supports English, German and French (intl. recommended) notation. More information about [decimal separators](https://en.wikipedia.org/wiki/Decimal_separator).

English is default/fallback for wrong or missing language code. Feels free to open an issue and request more or other notations.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jagottsicher/myGoToolbox

go 1.19
go 1.13
12 changes: 9 additions & 3 deletions thousands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ import (
"strings"
)

// Separate takes a numerical value and a language code and returns a string with thousands separators as commonly used in this lang/region. Actually Separate supports English, German and French (intl. recommended) notation. Invalid or empty langugae codes language code return English notation as default/fallback. Scientific notation for small and big numbers is taken care of.
// Separate takes a numerical value and a language code and returns a string with thousands separators as commonly used in this lang/region. Actually supports English, German and French (intl. recommended) notation. Invalid or empty language codes let Separate return English notation as default/fallback. Scientific notation for very small or very big numbers is taken care of.
func Separate(N interface{}, lang ...string) (string, error) {

// handles all numeric types, also unsigneds (and byte and rune as they're aliases)
switch N.(type) {
case int:
case uint:
case int16:
case int32:
case int64:
case int8:
case uint16:
case uint32:
case uint64:
case uint8:
case float32:
case float64:
default:
Expand Down Expand Up @@ -70,7 +76,7 @@ func Separate(N interface{}, lang ...string) (string, error) {
return n, nil

case "fr":
// TO-DO space as separator
// TO-DO space as separator some more sophisticated
n = strings.ReplaceAll(n, ",", ".")

dec := ""
Expand All @@ -97,7 +103,7 @@ func Separate(N interface{}, lang ...string) (string, error) {
if dec != "" {
n = n + "," + dec
}

// like German but exchange all "." to whitespaces. some lazy, I know :)
n = strings.ReplaceAll(n, ".", " ")

return n, nil
Expand Down

0 comments on commit 8550419

Please sign in to comment.