adding percent value format

This commit is contained in:
Will Charczuk 2016-07-14 11:21:41 -07:00
parent e7c2568a69
commit 1cbfb53b11
2 changed files with 11 additions and 0 deletions

View file

@ -59,6 +59,8 @@ const (
DefaultDateHourFormat = "01-02 3pm" DefaultDateHourFormat = "01-02 3pm"
// DefaultFloatFormat is the default float format. // DefaultFloatFormat is the default float format.
DefaultFloatFormat = "%.2f" DefaultFloatFormat = "%.2f"
// DefaultPercentValueFormat is the default percent format.
DefaultPercentValueFormat = "%0.2f%%"
) )
var ( var (

View file

@ -37,6 +37,15 @@ func FloatValueFormatter(v interface{}) string {
return FloatValueFormatterWithFormat(v, DefaultFloatFormat) 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. // FloatValueFormatterWithFormat is a ValueFormatter for float64 with a given format.
func FloatValueFormatterWithFormat(v interface{}, floatFormat string) string { func FloatValueFormatterWithFormat(v interface{}, floatFormat string) string {
if typed, isTyped := v.(float64); isTyped { if typed, isTyped := v.(float64); isTyped {