2024-09-27 00:11:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-28 07:46:18 +00:00
|
|
|
"html/template"
|
2024-09-27 00:11:04 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ScientificNotationValue float64
|
|
|
|
|
|
|
|
func (s *ScientificNotationValue) UnmarshalText(text []byte) error {
|
|
|
|
f, err := strconv.ParseFloat(string(text), 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*s = ScientificNotationValue(f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-28 07:46:18 +00:00
|
|
|
// formatMbarHTML formats a millibar value using scientific notation and returns
|
|
|
|
// a HTML fragment (for superscript support).
|
|
|
|
func formatMbarHTML(v float32) template.HTML {
|
|
|
|
exp := 0
|
|
|
|
for v < 1 {
|
|
|
|
v *= 10
|
|
|
|
exp -= 1
|
|
|
|
}
|
|
|
|
for v >= 10 {
|
|
|
|
v /= 10
|
|
|
|
exp += 1
|
|
|
|
}
|
|
|
|
res := fmt.Sprintf("%.3f", v)
|
|
|
|
res += fmt.Sprintf(" x 10<sup>%d</sup>", exp)
|
|
|
|
res += " mbar"
|
|
|
|
return template.HTML(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatMbar(v float32) string {
|
2024-09-27 00:11:04 +00:00
|
|
|
exp := 0
|
|
|
|
for v < 1 {
|
|
|
|
v *= 10
|
|
|
|
exp -= 1
|
|
|
|
}
|
|
|
|
for v >= 10 {
|
|
|
|
v /= 10
|
|
|
|
exp += 1
|
|
|
|
}
|
|
|
|
res := fmt.Sprintf("%.3f", v)
|
|
|
|
res += fmt.Sprintf("e%d", exp)
|
2024-09-28 07:46:18 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScientificNotationValue) MarshalText() ([]byte, error) {
|
|
|
|
v := float32(*s)
|
|
|
|
res := formatMbar(v)
|
2024-09-27 00:11:04 +00:00
|
|
|
return []byte(res), nil
|
|
|
|
}
|