FormatNumber formats n, aligned, in `len(unit) + 10` or fewer characters (except for extremely large numbers). It returns strings representing the numeral and the unit string.
(n uint64, unit string)
| 61 | // characters (except for extremely large numbers). It returns strings |
| 62 | // representing the numeral and the unit string. |
| 63 | func (h *Humaner) FormatNumber(n uint64, unit string) (numeral string, unitString string) { |
| 64 | prefix := h.prefixes[0] |
| 65 | |
| 66 | wholePart := n |
| 67 | for _, p := range h.prefixes { |
| 68 | w := n / p.Multiplier |
| 69 | if w >= 1 { |
| 70 | wholePart = w |
| 71 | prefix = p |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if prefix.Multiplier == 1 { |
| 76 | return fmt.Sprintf("%d", n), unit |
| 77 | } |
| 78 | |
| 79 | mantissa := float64(n) / float64(prefix.Multiplier) |
| 80 | var format string |
| 81 | |
| 82 | switch { |
| 83 | case wholePart >= 100: |
| 84 | // `mantissa` can actually be up to 1023.999. |
| 85 | format = "%.0f" |
| 86 | case wholePart >= 10: |
| 87 | format = "%.1f" |
| 88 | default: |
| 89 | format = "%.2f" |
| 90 | } |
| 91 | |
| 92 | return fmt.Sprintf(format, mantissa), prefix.Name + unit |
| 93 | } |
| 94 | |
| 95 | // Format formats values, aligned, in `len(unit) + 10` or fewer |
| 96 | // characters (except for extremely large numbers). It returns strings |
no outgoing calls