Skip to content

Commit 36dcc38

Browse files
authored
Merge pull request #114 from Shopify/make-decimal-implement-display
Make Decimal implement Display
2 parents 74d15e8 + eb2770f commit 36dcc38

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

shopify_function/src/scalars/decimal.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::{Deserialize, Serialize};
2+
use std::fmt;
23
use std::ops::Deref;
34

45
/// Convenience wrapper for converting between Shopify's `Decimal` scalar, which
@@ -15,6 +16,12 @@ impl Decimal {
1516
}
1617
}
1718

19+
impl fmt::Display for Decimal {
20+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21+
f.write_str(ryu::Buffer::new().format(self.0))
22+
}
23+
}
24+
1825
impl Deref for Decimal {
1926
type Target = f64;
2027

@@ -80,4 +87,12 @@ mod tests {
8087
let json_value = serde_json::to_value(decimal).expect("Error serializing to JSON");
8188
assert_eq!(serde_json::json!("123.4"), json_value);
8289
}
90+
91+
#[test]
92+
fn test_display_formatting() {
93+
assert_eq!(Decimal(123.45).to_string(), "123.45");
94+
assert_eq!(Decimal(123.0).to_string(), "123.0");
95+
assert_eq!(Decimal(0.0).to_string(), "0.0");
96+
assert_eq!(Decimal(-5.678).to_string(), "-5.678");
97+
}
8398
}

0 commit comments

Comments
 (0)