-
Notifications
You must be signed in to change notification settings - Fork 38
/
volume.go
30 lines (24 loc) · 886 Bytes
/
volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package unit
import (
"fmt"
)
// CubicFeet represents cubic feet
type CubicFeet float64
// CubicThousandthInch represents values in cubic thousandths of an inch
type CubicThousandthInch int
// truncateFloat truncates a float to 2 decimal places
func truncateFloat(f float64) float64 {
return float64(int(f*100)) / 100
}
// String converts a CubicFeet value into a string
func (c CubicFeet) String() string {
// truncate to 2 decimal places
truncatedValue := truncateFloat(float64(c))
// convert truncatedValue to a string
return fmt.Sprintf("%.2f", truncatedValue)
}
// ToCubicFeet converts cubic thousandths of an inch to cubic feet
func (cti CubicThousandthInch) ToCubicFeet() CubicFeet {
cubicThousandthInchPerCubicFoot := thousandthInchPerFoot * thousandthInchPerFoot * thousandthInchPerFoot
return CubicFeet(float64(cti) / float64(cubicThousandthInchPerCubicFoot))
}