Skip to content

Commit

Permalink
Merge pull request #114 from Shopify/make-decimal-implement-display
Browse files Browse the repository at this point in the history
Make Decimal implement Display
  • Loading branch information
Arkham authored Jan 31, 2025
2 parents 74d15e8 + eb2770f commit 36dcc38
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions shopify_function/src/scalars/decimal.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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");
}
}

0 comments on commit 36dcc38

Please sign in to comment.