10

I am printing floating point value(eg: 52.12) like this:

fmt.Sprintf("%.2f%s", percentageValue, "%%")

Output is like 52.12%. But I want to print it in other language than English where decimal point is comma ,. How to do it in Go using fmt.Sprintf. I want output like this 52,12% .

icza
  • 389,944
  • 63
  • 907
  • 827
Jai Prak
  • 2,855
  • 4
  • 29
  • 37
  • 1
    Possible duplicate of [How to fmt.Printf an integer with thousands comma](https://stackoverflow.com/questions/13020308/how-to-fmt-printf-an-integer-with-thousands-comma) – Marc Jan 05 '18 at 10:14
  • 1
    The short answer (as mentioned in the question linked above) is to use https://godoc.org/golang.org/x/text/message – Marc Jan 05 '18 at 10:16
  • The standard lib (and the `fmt` package) does not support localized text and number formatting. If you want that, take a look at the [`golang.org/x/text/message`](https://godoc.org/golang.org/x/text/message) package which _"implements formatted I/O for localized strings with functions analogous to the fmt's print functions. It is a drop-in replacement for fmt."_ – icza Jan 05 '18 at 10:16

4 Answers4

13

The fmt package does not support the functionality to replace the delimiter in a floating point number. You should instead use the golang.org/x/text/message package, which is designed for this purpose.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • I tried that package. But it doesn't work. Even on its front page, it uses the wrong decimal separator for Dutch. Who are the maintainers of this package? – Svip Sep 12 '18 at 12:37
  • That package is maintained by the official Go team. – Jonathan Hall Sep 12 '18 at 12:38
  • Thank you. It turns out that `message.MatchLanguage()` does not work. For whatever reason, `message.MatchLanguage("da")` does not return `language.Danish` (as one would expect) (or `language.Dutch` for "nl"), but rather the "und" `Tag`. – Svip Sep 12 '18 at 12:48
7

The standard lib (and the fmt package) does not support localized text and number formatting.
If you only need to localize the decimal point, you may use the easy way to simply replace the dot (.) with the comma character (,) like this:

percentageValue := 52.12
s := fmt.Sprintf("%.2f%%", percentageValue)
s = strings.Replace(s, ".", ",", -1)
fmt.Println(s)

(Also note that you may output a percent sign % by using 2 percent signs %% in the format string.)

Which outputs (try it on the Go Playground):

52,12%

Or with a mapping function:

func dot2comma(r rune) rune {
    if r == '.' {
        return ','
    }
    return r
}

func main() {
    percentageValue := 52.12
    s := fmt.Sprintf("%.2f%%", percentageValue)
    s = strings.Map(dot2comma, s)
    fmt.Println(s)
}

Output is the same. Try this one on the Go Playground.

Yet another solution could be to format the integer and fraction part separately, and glue them together with a comma , sign:

percentageValue := 52.12
i, f := math.Modf(percentageValue)
s := fmt.Sprint(i) + "," + fmt.Sprintf("%.2f%%", f)[2:]
fmt.Println(s)

Try this one on the Go Playground.

Note that this latter solution needs "adjusting" if the percent value is negative:

percentageValue := -52.12
i, f := math.Modf(percentageValue)
if f < 0 {
    f = -f
}
s := fmt.Sprint(i) + "," + fmt.Sprintf("%.2f%%", f)[2:]
fmt.Println(s)

This modified version will now print -52,12% properly. Try it on the Go Playground.

If you need "full" localization support, then do check out and use golang.org/x/text/message, which "implements formatted I/O for localized strings with functions analogous to the fmt's print functions. It is a drop-in replacement for fmt."

icza
  • 389,944
  • 63
  • 907
  • 827
1

I found a library to do that, it's called humanize. With this you can do something like this fmt.Println(humanize.FormatFloat("00,00", 52.20))

See in the link below the instructions: https://github.com/dustin/go-humanize

dvaltrick
  • 112
  • 2
  • 7
0

I have solved the problem by this:

func getFormattedValue(percentageValue float64) string{
  value := fmt.Sprintf("%.2f%s", percentageValue, "%")
  return strings.Replace(value, ".", ",", -1)
}

I am taking float value and converting to string then replacing . with ,

Jai Prak
  • 2,855
  • 4
  • 29
  • 37
  • This is the solution I posted in my answer. I don't think we need this duplicate answer. – icza Jan 07 '18 at 09:02