fmt.Printf & decimal commas

2,836 views
Skip to first unread message

HaWe

unread,
Jun 26, 2015, 6:55:50 AM6/26/15
to golan...@googlegroups.com
The fmt package provides Printf & friends which use decimal point to format floating point numbers.
Some languages  favour decimal commas instead.

Is there a way to tell Prinf to use decimal commas? (I didn't find any)
Are there oher ways to achieve this?
How did you do it? (if you did it)

Thanks in advance.

Will Donnelly

unread,
Jun 26, 2015, 5:13:20 PM6/26/15
to golan...@googlegroups.com
If you want internationalization features you'll need to use a proper internationalization library.

I've never used any of these, but top results for "golang i18n" include:

And at least one of those (https://godoc.org/github.com/vube/i18n) definitely supports formatting decimals with commas, I checked and the locale data tables in the repository specify the separator to be used.

HaWe

unread,
Jun 26, 2015, 7:26:21 PM6/26/15
to golan...@googlegroups.com
Silly me. I wanted this feature for my own i18n solution. But then forgot to check what others have done. I'll have a closer look now.
Thanks again.

Nigel Tao

unread,
Jun 27, 2015, 2:04:46 AM6/27/15
to HaWe, Marcel van Lohuizen, golang-nuts
On Fri, Jun 26, 2015 at 8:55 PM, HaWe <hwhe...@bitloeffel.de> wrote:
> The fmt package provides Printf & friends which use decimal point to format
> floating point numbers.
> Some languages favour decimal commas instead.
>
> Is there a way to tell Prinf to use decimal commas? (I didn't find any)

I ask mpvl all my Go i18n questions.

HaWe

unread,
Jun 27, 2015, 4:00:49 AM6/27/15
to golan...@googlegroups.com
> I ask mpvl all my Go i18n questions.

Awesome. Lots of code there. Intimidating. But thanks anyway. I'll look into it.

Tong Sun

unread,
Jun 27, 2015, 2:21:05 PM6/27/15
to golan...@googlegroups.com


On Saturday, June 27, 2015 at 4:00:49 AM UTC-4, HaWe wrote:
> I ask mpvl all my Go i18n questions.

Awesome. Lots of code there. Intimidating. But thanks anyway. I'll look into it.

Sorry, where is it, the mpvl which has lots of i18n code? 

I tried to find it myself, but didn't find any. 

Matt Harden

unread,
Jun 27, 2015, 3:10:05 PM6/27/15
to Tong Sun, golan...@googlegroups.com
mpvl is a person on the golang-nuts and golang-dev google groups.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

HaWe

unread,
Jun 27, 2015, 6:27:30 PM6/27/15
to golan...@googlegroups.com
> Sorry, where is it, the mpvl which has lots of i18n code? 

Sorry, I forgot the link. Here it is:
https://github.com/mpvl
And then: golang/text

Tong Sun

unread,
Jun 27, 2015, 7:01:21 PM6/27/15
to HaWe, golang-nuts
Thanks a lot HaWe.

Danver Braganza

unread,
Jun 29, 2015, 12:08:08 PM6/29/15
to golan...@googlegroups.com
Also look at Go-Humanize. It has exactly this feature, and is fairly small.

Danver Braganza

unread,
Jun 29, 2015, 12:08:27 PM6/29/15
to golan...@googlegroups.com

HaWe

unread,
Jun 29, 2015, 12:27:43 PM6/29/15
to golan...@googlegroups.com
go-humanize is a fine package, indeed. Thanks for the pointer.

Alas, for the particurlar issue ("decimal coma") I decided to simply combine fmt.Sprintf & strings.Replace. Furthermore strings.TrimRight helps to get rid of trailing zeros.

HaWe

unread,
Jun 29, 2015, 12:32:58 PM6/29/15
to golan...@googlegroups.com
strings.Replace(lastPost, "coma", "comma", -1)
Reply all
Reply to author
Forward
0 new messages