File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed
shopify_function/src/scalars Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 11use serde:: { Deserialize , Serialize } ;
2+ use std:: fmt;
23use 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+
1825impl 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}
You can’t perform that action at this time.
0 commit comments