-1

In go, the statement

fmt.Sprintf("%4.3f", 18.8836)

Will return

18.883

Is there a way of making the return value

18.88

In other words, is there a way to make the 4 apply to the whole width of the number and not just the decimal portion?

Ryan
  • 1,131
  • 3
  • 13
  • 17
  • Related: [Golang: is there any standard library to convert float64 to string with fix width with maximum number of significant digits?](https://stackoverflow.com/questions/36515818/golang-is-there-any-standard-library-to-convert-float64-to-string-with-fix-widt/36518167#36518167) – icza Jun 15 '17 at 02:47

1 Answers1

3

You can accomplish this with %g:

fmt.Printf("%.4g\n", 18.8836)

https://play.golang.org/p/accWeNXG9V

user94559
  • 59,196
  • 6
  • 103
  • 103