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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::measurement::*;
#[derive(Copy, Clone, Debug)]
pub struct Weight {
kilograms: f64,
}
impl Weight {
pub fn from_kilograms(kilograms: f64) -> Self {
Weight { kilograms: kilograms }
}
pub fn from_micrograms(micrograms: f64) -> Self {
Self::from_kilograms(micrograms / 1000000000.0)
}
pub fn from_milligrams(milligrams: f64) -> Self {
Self::from_kilograms(milligrams / 1000000.0)
}
pub fn from_carats(carats: f64) -> Self {
Self::from_kilograms(carats / 5000.0)
}
pub fn from_grams(grams: f64) -> Self {
Self::from_kilograms(grams / 1000.0)
}
pub fn from_metric_tons(metric_tons: f64) -> Self {
Self::from_kilograms(metric_tons * 1000.0)
}
pub fn from_grains(grains: f64) -> Self {
Self::from_kilograms(grains / 15432.358)
}
pub fn from_pennyweights(pennyweights: f64) -> Self {
Self::from_kilograms(pennyweights / 643.01493)
}
pub fn from_ounces(ounces: f64) -> Self {
Self::from_kilograms(ounces / 35.273962)
}
pub fn from_troy_ounces(troy_ounces: f64) -> Self {
Self::from_kilograms(troy_ounces / 32.150747)
}
pub fn from_pounds(pounds: f64) -> Self {
Self::from_kilograms(pounds / 2.2046228)
}
pub fn from_troy_pounds(troy_pounds: f64) -> Self {
Self::from_kilograms(troy_pounds / 2.6792289)
}
pub fn from_stones(stones: f64) -> Self {
Self::from_kilograms(stones / 0.15747304)
}
pub fn from_short_tons(short_tons: f64) -> Self {
Self::from_kilograms(short_tons * 907.18475)
}
pub fn from_long_tons(long_tons: f64) -> Self {
Self::from_kilograms(long_tons * 1016.0469)
}
pub fn as_micrograms(&self) -> f64 {
self.kilograms * 1000000000.0
}
pub fn as_milligrams(&self) -> f64 {
self.kilograms * 1000000.0
}
pub fn as_carats(&self) -> f64 {
self.kilograms * 5000.0
}
pub fn as_grams(&self) -> f64 {
self.kilograms * 1000.0
}
pub fn as_kilograms(&self) -> f64 {
self.kilograms
}
pub fn as_metric_tons(&self) -> f64 {
self.kilograms / 1000.0
}
pub fn as_grains(&self) -> f64 {
self.kilograms * 15432.358
}
pub fn as_pennyweights(&self) -> f64 {
self.kilograms * 643.01493
}
pub fn as_ounces(&self) -> f64 {
self.kilograms * 35.273962
}
pub fn as_pounds(&self) -> f64 {
self.kilograms * 2.2046228
}
pub fn as_troy_ounces(&self) -> f64 {
self.kilograms * 32.150747
}
pub fn as_troy_pounds(&self) -> f64 {
self.kilograms * 2.6792289
}
pub fn as_stones(&self) -> f64 {
self.kilograms * 0.15747304
}
pub fn as_short_tons(&self) -> f64 {
self.kilograms / 907.18475
}
pub fn as_long_tons(&self) -> f64 {
self.kilograms / 1016.0469
}
}
impl Measurement for Weight {
fn get_base_units(&self) -> f64 {
self.kilograms
}
fn from_base_units(units: f64) -> Self {
Self::from_kilograms(units)
}
}
implement_measurement! { Weight }
impl ::std::fmt::Display for Weight {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:.1} kg", self.as_kilograms())
}
}