2023-06-07 12:10:05 +05:30

29 lines
636 B
Go

package formatter
import "fmt"
type percentFormatter struct{}
func NewPercentFormatter() Formatter {
return &percentFormatter{}
}
func toPercent(value float64, decimals DecimalCount) string {
return toFixed(value, decimals) + "%"
}
func toPercentUnit(value float64, decimals DecimalCount) string {
return toFixed(value*100, decimals) + "%"
}
func (f *percentFormatter) Format(value float64, unit string) string {
switch unit {
case "percent":
return toPercent(value, nil)
case "percentunit":
return toPercentUnit(value, nil)
}
// When unit is not matched, return the value as it is.
return fmt.Sprintf("%v", value)
}