Go: Convert float to string
Use the fmt.Sprintf method to convert a floating-point number to a string.
s := fmt.Sprintf("%f", 123.456) // s == "123.456000"
| Formatting | Description | Verb |
|---|---|---|
| 1.234560e+02 | Scientific notation | %e |
| 123.456000 | Decimal point, no exponent | %f |
| 123.46 | Default width, precision 2 | %.2f |
| ␣␣123.46 | Width 8, precision 2 | %8.2f |
| 123.456 | Exponent as needed, necessary digits only | %g |
Comments
Be the first to comment!