website/pkg/utility/number_format.go
Noah Petherbridge 3fdae1d8d7 Various minor tweaks
* Demographics page:
    * Show percents with up to 1 decimal place of precision.
    * On tablets+ align the percent text to the right.
    * On photo counts, only include certified active user photos.
    * On gender/orientation demographics, pad the remaining "No answer" counts
      with the set of users who have no profile_fields set in the database yet.
* Admin certification page:
    * Add additional "common rejection reasons"
    * Add a confirm prompt when viewing the Rejected list to avoid accidental
      approval of previously rejected cert photos.
2024-09-27 17:37:45 -07:00

44 lines
1.0 KiB
Go

package utility
import (
"fmt"
"strconv"
"strings"
)
// FormatFloatToPrecision will trim a floating point number to at most a number of decimals of precision.
//
// If the precision is ".0" the decimal place will be stripped entirely.
func FormatFloatToPrecision(v float64, prec int) string {
s := strconv.FormatFloat(v, 'f', prec, 64)
if strings.HasSuffix(s, ".0") {
return strings.Split(s, ".")[0]
}
return s
}
// FormatNumberShort compresses a number into as short a string as possible (e.g. "1.2K" when it gets into the thousands).
func FormatNumberShort(value int64) string {
// Under 1,000?
if value < 1000 {
return fmt.Sprintf("%d", value)
}
// Start to bucket it.
var (
thousands = float64(value) / 1000
millions = float64(thousands) / 1000
billions = float64(millions) / 1000
)
if thousands < 1000 {
return fmt.Sprintf("%sK", FormatFloatToPrecision(thousands, 1))
}
if millions < 1000 {
return fmt.Sprintf("%sM", FormatFloatToPrecision(millions, 1))
}
return fmt.Sprintf("%sB", FormatFloatToPrecision(billions, 1))
}