From 895ae394471e6f22440cd7b0e30f6af5e25b4734 Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Tue, 23 Sep 2025 10:51:11 +0200 Subject: [PATCH] Fix sign and padding in string format for negative Numeric --- src/tds/numeric.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/tds/numeric.rs b/src/tds/numeric.rs index 4f856beb..0e23b6b7 100644 --- a/src/tds/numeric.rs +++ b/src/tds/numeric.rs @@ -186,9 +186,10 @@ impl Debug for Numeric { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { write!( f, - "{}.{:0pad$}", - self.int_part(), - self.dec_part(), + "{}{}.{:0pad$}", + if self.value() < 0 { "-" } else { "" }, + self.int_part().abs(), + self.dec_part().abs(), pad = self.scale as usize ) } @@ -368,6 +369,24 @@ mod tests { assert_eq!(n.dec_part(), 5); } + #[test] + fn numeric_to_string() { + assert_eq!(Numeric::new_with_scale(123, 0).to_string(), "123.0"); + assert_eq!(Numeric::new_with_scale(123, 1).to_string(), "12.3"); + assert_eq!(Numeric::new_with_scale(123, 2).to_string(), "1.23"); + assert_eq!(Numeric::new_with_scale(123, 3).to_string(), "0.123"); + assert_eq!(Numeric::new_with_scale(123, 4).to_string(), "0.0123"); + assert_eq!(Numeric::new_with_scale(123, 36).to_string(), "0.000000000000000000000000000000000123"); + assert_eq!(Numeric::new_with_scale(123, 37).to_string(), "0.0000000000000000000000000000000000123"); + assert_eq!(Numeric::new_with_scale(-123, 0).to_string(), "-123.0"); + assert_eq!(Numeric::new_with_scale(-123, 1).to_string(), "-12.3"); + assert_eq!(Numeric::new_with_scale(-123, 2).to_string(), "-1.23"); + assert_eq!(Numeric::new_with_scale(-123, 3).to_string(), "-0.123"); + assert_eq!(Numeric::new_with_scale(-123, 4).to_string(), "-0.0123"); + assert_eq!(Numeric::new_with_scale(-123, 36).to_string(), "-0.000000000000000000000000000000000123"); + assert_eq!(Numeric::new_with_scale(-123, 37).to_string(), "-0.0000000000000000000000000000000000123"); + } + #[test] fn calculates_precision_correctly() { let n = Numeric::new_with_scale(57705, 2);