diff --git a/shopify_function/src/scalars/decimal.rs b/shopify_function/src/scalars/decimal.rs index d296d85..9f08939 100644 --- a/shopify_function/src/scalars/decimal.rs +++ b/shopify_function/src/scalars/decimal.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use std::fmt; use std::ops::Deref; /// Convenience wrapper for converting between Shopify's `Decimal` scalar, which @@ -15,6 +16,12 @@ impl Decimal { } } +impl fmt::Display for Decimal { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(ryu::Buffer::new().format(self.0)) + } +} + impl Deref for Decimal { type Target = f64; @@ -80,4 +87,12 @@ mod tests { let json_value = serde_json::to_value(decimal).expect("Error serializing to JSON"); assert_eq!(serde_json::json!("123.4"), json_value); } + + #[test] + fn test_display_formatting() { + assert_eq!(Decimal(123.45).to_string(), "123.45"); + assert_eq!(Decimal(123.0).to_string(), "123.0"); + assert_eq!(Decimal(0.0).to_string(), "0.0"); + assert_eq!(Decimal(-5.678).to_string(), "-5.678"); + } }