You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
403 B

  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. }