@@ -34,9 +34,9 @@ pub struct Decimal256RangeExceeded;
3434
3535impl Decimal256 {
3636 const DECIMAL_FRACTIONAL : Uint256 = // 1*10**18
37- Uint256 :: from_u128 ( 1_000_000_000_000_000_000 ) ;
37+ Uint256 :: new ( 1_000_000_000_000_000_000 ) ;
3838 const DECIMAL_FRACTIONAL_SQUARED : Uint256 = // 1*10**36
39- Uint256 :: from_u128 ( 1_000_000_000_000_000_000_000_000_000_000_000_000 ) ;
39+ Uint256 :: new ( 1_000_000_000_000_000_000_000_000_000_000_000_000 ) ;
4040
4141 /// The number of decimal places. Since decimal types are fixed-point rather than
4242 /// floating-point, this is a constant.
@@ -48,14 +48,29 @@ impl Decimal256 {
4848
4949 /// Creates a Decimal256 from Uint256
5050 /// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
51+ ///
52+ /// ## Examples
53+ ///
54+ /// ```
55+ /// # use cosmwasm_std::{Uint256, Decimal256};
56+ /// let atoms = Uint256::new(141_183_460_469_231_731_687_303_715_884_105_727_125);
57+ /// let value = Decimal256::new(atoms);
58+ /// assert_eq!(value.to_string(), "141183460469231731687.303715884105727125");
59+ /// ```
60+ #[ inline]
61+ #[ must_use]
5162 pub const fn new ( value : Uint256 ) -> Self {
5263 Self ( value)
5364 }
5465
5566 /// Creates a Decimal256 from u128
5667 /// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
68+ #[ deprecated(
69+ since = "3.0.0" ,
70+ note = "Use Decimal256::new(Uint256::new(value)) instead"
71+ ) ]
5772 pub const fn raw ( value : u128 ) -> Self {
58- Self ( Uint256 :: from_u128 ( value) )
73+ Self ( Uint256 :: new ( value) )
5974 }
6075
6176 /// Create a 1.0 Decimal256
@@ -84,7 +99,7 @@ impl Decimal256 {
8499 pub const fn percent ( x : u64 ) -> Self {
85100 // multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
86101 let atomics = ( x as u128 ) * 10_000_000_000_000_000 ;
87- Self ( Uint256 :: from_u128 ( atomics) )
102+ Self ( Uint256 :: new ( atomics) )
88103 }
89104
90105 /// Convert permille (x/1000) into Decimal256
@@ -101,7 +116,7 @@ impl Decimal256 {
101116 pub const fn permille ( x : u64 ) -> Self {
102117 // multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
103118 let atomics = ( x as u128 ) * 1_000_000_000_000_000 ;
104- Self ( Uint256 :: from_u128 ( atomics) )
119+ Self ( Uint256 :: new ( atomics) )
105120 }
106121
107122 /// Convert basis points (x/10000) into Decimal256
@@ -120,7 +135,7 @@ impl Decimal256 {
120135 pub const fn bps ( x : u64 ) -> Self {
121136 // multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
122137 let atomics = ( x as u128 ) * 100_000_000_000_000 ;
123- Self ( Uint256 :: from_u128 ( atomics) )
138+ Self ( Uint256 :: new ( atomics) )
124139 }
125140
126141 /// Creates a decimal from a number of atomic units and the number
@@ -798,6 +813,7 @@ mod tests {
798813 }
799814
800815 #[ test]
816+ #[ allow( deprecated) ]
801817 fn decimal256_raw ( ) {
802818 let value = 300u128 ;
803819 let expected = Uint256 :: from ( value) ;
@@ -2218,18 +2234,18 @@ mod tests {
22182234 #[ test]
22192235 fn decimal256_to_uint_floor_works ( ) {
22202236 let d = Decimal256 :: from_str ( "12.000000000000000001" ) . unwrap ( ) ;
2221- assert_eq ! ( d. to_uint_floor( ) , Uint256 :: from_u128 ( 12 ) ) ;
2237+ assert_eq ! ( d. to_uint_floor( ) , Uint256 :: new ( 12 ) ) ;
22222238 let d = Decimal256 :: from_str ( "12.345" ) . unwrap ( ) ;
2223- assert_eq ! ( d. to_uint_floor( ) , Uint256 :: from_u128 ( 12 ) ) ;
2239+ assert_eq ! ( d. to_uint_floor( ) , Uint256 :: new ( 12 ) ) ;
22242240 let d = Decimal256 :: from_str ( "12.999" ) . unwrap ( ) ;
2225- assert_eq ! ( d. to_uint_floor( ) , Uint256 :: from_u128 ( 12 ) ) ;
2241+ assert_eq ! ( d. to_uint_floor( ) , Uint256 :: new ( 12 ) ) ;
22262242 let d = Decimal256 :: from_str ( "0.98451384" ) . unwrap ( ) ;
2227- assert_eq ! ( d. to_uint_floor( ) , Uint256 :: from_u128 ( 0 ) ) ;
2243+ assert_eq ! ( d. to_uint_floor( ) , Uint256 :: new ( 0 ) ) ;
22282244
22292245 let d = Decimal256 :: from_str ( "75.0" ) . unwrap ( ) ;
2230- assert_eq ! ( d. to_uint_floor( ) , Uint256 :: from_u128 ( 75 ) ) ;
2246+ assert_eq ! ( d. to_uint_floor( ) , Uint256 :: new ( 75 ) ) ;
22312247 let d = Decimal256 :: from_str ( "0.0" ) . unwrap ( ) ;
2232- assert_eq ! ( d. to_uint_floor( ) , Uint256 :: from_u128 ( 0 ) ) ;
2248+ assert_eq ! ( d. to_uint_floor( ) , Uint256 :: new ( 0 ) ) ;
22332249
22342250 let d = Decimal256 :: MAX ;
22352251 assert_eq ! (
@@ -2267,16 +2283,16 @@ mod tests {
22672283 #[ test]
22682284 fn decimal256_to_uint_ceil_works ( ) {
22692285 let d = Decimal256 :: from_str ( "12.000000000000000001" ) . unwrap ( ) ;
2270- assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: from_u128 ( 13 ) ) ;
2286+ assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: new ( 13 ) ) ;
22712287 let d = Decimal256 :: from_str ( "12.345" ) . unwrap ( ) ;
2272- assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: from_u128 ( 13 ) ) ;
2288+ assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: new ( 13 ) ) ;
22732289 let d = Decimal256 :: from_str ( "12.999" ) . unwrap ( ) ;
2274- assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: from_u128 ( 13 ) ) ;
2290+ assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: new ( 13 ) ) ;
22752291
22762292 let d = Decimal256 :: from_str ( "75.0" ) . unwrap ( ) ;
2277- assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: from_u128 ( 75 ) ) ;
2293+ assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: new ( 75 ) ) ;
22782294 let d = Decimal256 :: from_str ( "0.0" ) . unwrap ( ) ;
2279- assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: from_u128 ( 0 ) ) ;
2295+ assert_eq ! ( d. to_uint_ceil( ) , Uint256 :: new ( 0 ) ) ;
22802296
22812297 let d = Decimal256 :: MAX ;
22822298 assert_eq ! (
0 commit comments