73

http://golang.org/pkg/strconv/

http://play.golang.org/p/4VNRgW8WoB

How do I convert a float number into string format? This is google playground but not getting the expected output. (2e+07) I want to get "21312421.213123"

package main

import "fmt"
import "strconv"

func floattostr(input_num float64) string {

        // to convert a float number to a string
    return strconv.FormatFloat(input_num, 'g', 1, 64)
 }

 func main() {
      fmt.Println(floattostr(21312421.213123))
      // what I expect is "21312421.213123" in string format
 }

Please help me get the string out of float number. Thanks

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • `fmt.Sprintf("%.6f", input_num)` to set a float to a string. See https://stackoverflow.com/a/62753031/12817546. `strconv.ParseFloat(f, 64)` to set a string to a float. See https://stackoverflow.com/a/62740786/12817546. –  Jul 09 '20 at 05:34

1 Answers1

161

Try this

package main

import "fmt"
import "strconv"

func FloatToString(input_num float64) string {
    // to convert a float number to a string
    return strconv.FormatFloat(input_num, 'f', 6, 64)
}

func main() {
    fmt.Println(FloatToString(21312421.213123))
}

If you just want as many digits precision as possible, then the special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly. Eg

strconv.FormatFloat(input_num, 'f', -1, 64)

Personally I find fmt easier to use. (Playground link)

fmt.Printf("x = %.6f\n", 21312421.213123)

Or if you just want to convert the string

fmt.Sprintf("%.6f", 21312421.213123)
Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132
  • 2
    +1 for `strconv.FormatFloat` - I wasn't aware of that, but it might be worth noting that `fmt`, while nicer to use, will be a tiny bit slower than the direct function call, because it has to do a lot of parsing just in order to figure out what you want, and only then call `strconv.FormatFloat`. – Alexander Bauer Sep 30 '13 at 20:19
  • Thanks but what if I do not know how precise it should be. What if I do not know it is .000001 or .001 –  Sep 30 '13 at 21:17
  • 3
    I updated the answer with a section on precision -1 in `FloatFormat` which is what you want. – Nick Craig-Wood Sep 30 '13 at 22:00
  • 1
    what is the 'f' in FormatFloat? i don't get it. – Chanlito Jul 26 '17 at 13:17
  • 2
    @Bruce the 'f' controls the format - see [the docs](https://golang.org/pkg/strconv/#FormatFloat) for more info. – Nick Craig-Wood Aug 07 '17 at 21:36
  • @NickCraig-Wood, any advice for preventing loss of precision when the float is quite large? E.g.: https://play.golang.org/p/1whVxuLbkJG – gpanda Jan 30 '18 at 19:26
  • 1
    @gpanda unfortunately you ran out of precision. Your number has 74 bits in whereas float64 can only store 56 bits :-( If you want more precision then look into [big.Int/big.Float](https://golang.org/pkg/math/big/) – Nick Craig-Wood Feb 01 '18 at 10:48
  • I have to say I like Sprintf better than FormatFloat, since it is probably easier to read to my fellow teammates that come from C background, where as the FormatFloat function takes too many parameters and I can't be bothered to actually figure out what they do, life is too short. – Felipe Valdes Mar 06 '18 at 02:36
  • This are complicated =/ where's a nice utility lib? – Jonathan Mar 16 '19 at 23:59