Explorar o código

科学计数法先转f64,再转Decimal

skyfffire %!s(int64=2) %!d(string=hai) anos
pai
achega
12f5ad9ec7
Modificáronse 1 ficheiros con 12 adicións e 1 borrados
  1. 12 1
      strategy/tests/decimal_test.rs

+ 12 - 1
strategy/tests/decimal_test.rs

@@ -1,8 +1,10 @@
 #[cfg(test)]
 mod tests {
     use std::cmp::{max, min};
+    use std::str::FromStr;
     use std::time::{Instant, Duration};
-    use rust_decimal::prelude::ToPrimitive;
+    use rust_decimal::Decimal;
+    use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
     use rust_decimal_macros::dec;
 
     #[test]
@@ -40,4 +42,13 @@ mod tests {
         println!("max({}, {})={}", a, b, max(a, b));
         println!("min({}, {})={}", a, b, min(a, b));
     }
+
+    #[test]
+    fn test_decimal_from() {
+        let a = dec!(1e-6);
+        let b = Decimal::from_f64(f64::from_str("1e-6").unwrap()).unwrap();
+
+        println!("a={}", a);
+        println!("b={:?}", b);
+    }
 }