mirror of
https://github.com/SigNoz/signoz.git
synced 2025-12-21 01:16:57 +00:00
29 lines
636 B
Go
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)
|
||
|
|
}
|