ftoa.go 403 B

1234567891011121314151617181920212223
  1. package humanize
  2. import "strconv"
  3. func stripTrailingZeros(s string) string {
  4. offset := len(s) - 1
  5. for offset > 0 {
  6. if s[offset] == '.' {
  7. offset--
  8. break
  9. }
  10. if s[offset] != '0' {
  11. break
  12. }
  13. offset--
  14. }
  15. return s[:offset+1]
  16. }
  17. // Ftoa converts a float to a string with no trailing zeros.
  18. func Ftoa(num float64) string {
  19. return stripTrailingZeros(strconv.FormatFloat(num, 'f', 6, 64))
  20. }