diff --git a/defaults.go b/defaults.go index f4e91d1..a635e6a 100644 --- a/defaults.go +++ b/defaults.go @@ -59,6 +59,8 @@ const ( DefaultDateHourFormat = "01-02 3pm" // DefaultFloatFormat is the default float format. DefaultFloatFormat = "%.2f" + // DefaultPercentValueFormat is the default percent format. + DefaultPercentValueFormat = "%0.2f%%" ) var ( diff --git a/value_formatter.go b/value_formatter.go index 4902125..a13ddaa 100644 --- a/value_formatter.go +++ b/value_formatter.go @@ -37,6 +37,15 @@ func FloatValueFormatter(v interface{}) string { return FloatValueFormatterWithFormat(v, DefaultFloatFormat) } +// PercentValueFormatter is a formatter for percent values. +// NOTE: it normalizes the values, i.e. multiplies by 100.0. +func PercentValueFormatter(v interface{}) string { + if typed, isTyped := v.(float64); isTyped { + return FloatValueFormatterWithFormat(typed*100.0, DefaultPercentValueFormat) + } + return "" +} + // FloatValueFormatterWithFormat is a ValueFormatter for float64 with a given format. func FloatValueFormatterWithFormat(v interface{}, floatFormat string) string { if typed, isTyped := v.(float64); isTyped {