@@ -35,6 +35,11 @@ type templates struct {
3535 exec func (string , interface {}) (string , error )
3636}
3737
38+ type hashrateDisplay struct {
39+ Parts []string
40+ Unit string
41+ }
42+
3843func newTemplates (folder string , reload bool , common []string , helpers template.FuncMap ) templates {
3944 com := make ([]string , 0 , len (common ))
4045 for _ , file := range common {
@@ -210,6 +215,34 @@ func amountAsDecimalPartsTrimmed(v, numPlaces int64, useCommas bool) []string {
210215 return []string {left , right , tail }
211216}
212217
218+ func hashRateFormatting (phRate float64 ) hashrateDisplay {
219+ units := []struct {
220+ label string
221+ multiplier float64
222+ }{
223+ {label : "Th/s" , multiplier : 1 },
224+ {label : "Ph/s" , multiplier : 1e-3 },
225+ {label : "Eh/s" , multiplier : 1e-6 },
226+ }
227+
228+ thRate := phRate * 1e3
229+ unitIndex := 0
230+ if thRate > 0 {
231+ unitIndex = int (math .Floor (math .Log10 (thRate ) / 3 ))
232+ if unitIndex < 0 {
233+ unitIndex = 0
234+ } else if unitIndex >= len (units ) {
235+ unitIndex = len (units ) - 1
236+ }
237+ }
238+
239+ value := thRate * units [unitIndex ].multiplier
240+ return hashrateDisplay {
241+ Parts : float64Formatting (value , 8 , true , 2 ),
242+ Unit : units [unitIndex ].label ,
243+ }
244+ }
245+
213246// threeSigFigs returns a representation of the float formatted to three
214247// significant figures, with an appropriate magnitude prefix (k, M, B).
215248// For (k, M, G) prefixes for file/memory sizes, use humanize.Bytes.
@@ -406,6 +439,7 @@ func makeTemplateFuncMap(params *chaincfg.Params) template.FuncMap {
406439 return p
407440 },
408441 "float64AsDecimalParts" : float64Formatting ,
442+ "hashRateFormatting" : hashRateFormatting ,
409443 "amountAsDecimalParts" : func (v int64 , useCommas bool ) []string {
410444 return float64Formatting (dcrutil .Amount (v ).ToCoin (), 8 , useCommas )
411445 },
0 commit comments