decimal icon indicating copy to clipboard operation
decimal copied to clipboard

Support for other decimal separators

Open emarj opened this issue 2 years ago • 0 comments

This library currently supports only the dot . as a decimal separator. Unfortunately there is not a standard for this (see here for example). The comma , for example, is also heavily used.

It would be nice to have some support from the library in regard to this. This issue falls under the umbrella of localization, which is quite tedious to implement, but some global variables could be enough.

At the moment the dot is hard-coded here https://github.com/shopspring/decimal/blob/f55dd564545cec84cf84f7a53fb3025cdbec1c4f/decimal.go#L159

It would be really easy to support others decimal separators by introducing a global variable

var DecimalSeparator string = "."

that users can change from outside, and use it there. Of course the code for formatting the number should also be modified.

Sometimes one wants to support different formats. At this point would be possible to also allow for several separators. For example

var DecimalSeparators string = ".," //Support for both dot and comma

and

	if strings.Contains(DecimalSeparators, value[i]) {

For the formatting, the first separator DecimalSeparators[0] could be used.

What do you think? I would like to hear some opinion before (eventually) submitting a PR.

emarj avatar Dec 05 '22 20:12 emarj