@@ -34,9 +34,9 @@ pub struct Decimal256RangeExceeded;
34
34
35
35
impl Decimal256 {
36
36
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 ) ;
38
38
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 ) ;
40
40
41
41
/// The number of decimal places. Since decimal types are fixed-point rather than
42
42
/// floating-point, this is a constant.
@@ -48,14 +48,29 @@ impl Decimal256 {
48
48
49
49
/// Creates a Decimal256 from Uint256
50
50
/// 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]
51
62
pub const fn new ( value : Uint256 ) -> Self {
52
63
Self ( value)
53
64
}
54
65
55
66
/// Creates a Decimal256 from u128
56
67
/// 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
+ ) ]
57
72
pub const fn raw ( value : u128 ) -> Self {
58
- Self ( Uint256 :: from_u128 ( value) )
73
+ Self ( Uint256 :: new ( value) )
59
74
}
60
75
61
76
/// Create a 1.0 Decimal256
@@ -84,7 +99,7 @@ impl Decimal256 {
84
99
pub const fn percent ( x : u64 ) -> Self {
85
100
// multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
86
101
let atomics = ( x as u128 ) * 10_000_000_000_000_000 ;
87
- Self ( Uint256 :: from_u128 ( atomics) )
102
+ Self ( Uint256 :: new ( atomics) )
88
103
}
89
104
90
105
/// Convert permille (x/1000) into Decimal256
@@ -101,7 +116,7 @@ impl Decimal256 {
101
116
pub const fn permille ( x : u64 ) -> Self {
102
117
// multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
103
118
let atomics = ( x as u128 ) * 1_000_000_000_000_000 ;
104
- Self ( Uint256 :: from_u128 ( atomics) )
119
+ Self ( Uint256 :: new ( atomics) )
105
120
}
106
121
107
122
/// Convert basis points (x/10000) into Decimal256
@@ -120,7 +135,7 @@ impl Decimal256 {
120
135
pub const fn bps ( x : u64 ) -> Self {
121
136
// multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
122
137
let atomics = ( x as u128 ) * 100_000_000_000_000 ;
123
- Self ( Uint256 :: from_u128 ( atomics) )
138
+ Self ( Uint256 :: new ( atomics) )
124
139
}
125
140
126
141
/// Creates a decimal from a number of atomic units and the number
@@ -798,6 +813,7 @@ mod tests {
798
813
}
799
814
800
815
#[ test]
816
+ #[ allow( deprecated) ]
801
817
fn decimal256_raw ( ) {
802
818
let value = 300u128 ;
803
819
let expected = Uint256 :: from ( value) ;
@@ -2218,18 +2234,18 @@ mod tests {
2218
2234
#[ test]
2219
2235
fn decimal256_to_uint_floor_works ( ) {
2220
2236
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 ) ) ;
2222
2238
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 ) ) ;
2224
2240
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 ) ) ;
2226
2242
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 ) ) ;
2228
2244
2229
2245
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 ) ) ;
2231
2247
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 ) ) ;
2233
2249
2234
2250
let d = Decimal256 :: MAX ;
2235
2251
assert_eq ! (
@@ -2267,16 +2283,16 @@ mod tests {
2267
2283
#[ test]
2268
2284
fn decimal256_to_uint_ceil_works ( ) {
2269
2285
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 ) ) ;
2271
2287
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 ) ) ;
2273
2289
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 ) ) ;
2275
2291
2276
2292
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 ) ) ;
2278
2294
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 ) ) ;
2280
2296
2281
2297
let d = Decimal256 :: MAX ;
2282
2298
assert_eq ! (
0 commit comments