dec_number/
decimal128.rs

1/*
2 * MIT License
3 *
4 * Copyright (c) 2021 senees
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25//! `Decimal128`
26
27use crate::dec_quad::DecQuad;
28use std::fmt::Debug;
29
30#[repr(C)]
31pub struct Decimal128(DecQuad);
32
33impl Decimal128 {
34  /// Returns new [Decimal128] set to zero.
35  pub fn zero() -> Self {
36    Self(DecQuad::zero())
37  }
38  /// Returns new [Decimal128] set to negative zero.
39  pub fn negative_zero() -> Self {
40    Self(DecQuad::negative_zero())
41  }
42  /// Returns the absolute value of this [Decimal128].
43  pub fn abs(&self) -> Self {
44    Self(self.0.abs())
45  }
46  /// Returns `true` if this [Decimal128] is less than zero and not a `NaN`, or `false` otherwise.
47  pub fn is_negative(&self) -> bool {
48    self.0.is_negative()
49  }
50  /// Returns `true` if this [Decimal128] has a sign, or `false` otherwise.
51  /// Note that zeros and NaNs may also have a sign.
52  pub fn is_signed(&self) -> bool {
53    self.0.is_signed()
54  }
55  /// Returns `true` if this [Decimal128] is a zero, or `false` otherwise.
56  pub fn is_zero(&self) -> bool {
57    self.0.is_zero()
58  }
59  /// Returns `true` if this [Decimal128] is a NaN (quiet or signaling), or `false` otherwise.
60  pub fn is_nan(&self) -> bool {
61    self.0.is_nan()
62  }
63}
64
65impl Default for Decimal128 {
66  /// The default value for [Decimal128] is positive zero.
67  fn default() -> Self {
68    Self(DecQuad::default())
69  }
70}
71
72impl Debug for Decimal128 {
73  /// Converts [Decimal128] to a string in the form of hexadecimal bytes separated with spaces.
74  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75    write!(f, "{:?}", self.0)
76  }
77}
78
79impl From<i8> for Decimal128 {
80  /// Returns a [Decimal128] initialized from [i8] value.
81  fn from(n: i8) -> Self {
82    Self(DecQuad::from(n))
83  }
84}
85
86impl From<u8> for Decimal128 {
87  /// Returns a [Decimal128] initialized from [u8] value.
88  fn from(n: u8) -> Self {
89    Self(DecQuad::from(n))
90  }
91}
92
93impl From<i16> for Decimal128 {
94  /// Returns a [Decimal128] initialized from [i16] value.
95  fn from(n: i16) -> Self {
96    Self(DecQuad::from(n))
97  }
98}
99
100impl From<u16> for Decimal128 {
101  /// Returns a [Decimal128] initialized from [u16] value.
102  fn from(n: u16) -> Self {
103    Self(DecQuad::from(n))
104  }
105}
106
107impl From<i32> for Decimal128 {
108  /// Returns a [Decimal128] initialized from [i32] value.
109  fn from(n: i32) -> Self {
110    Self(DecQuad::from(n))
111  }
112}
113
114impl From<u32> for Decimal128 {
115  /// Returns a [Decimal128] initialized from [u32] value.
116  fn from(n: u32) -> Self {
117    Self(DecQuad::from(n))
118  }
119}
120
121impl From<i64> for Decimal128 {
122  /// Returns a [Decimal128] initialized from [i64] value.
123  fn from(n: i64) -> Self {
124    Self(DecQuad::from(n))
125  }
126}
127
128impl From<u64> for Decimal128 {
129  /// Returns a [Decimal128] initialized from [u64] value.
130  fn from(n: u64) -> Self {
131    Self(DecQuad::from(n))
132  }
133}
134
135impl From<&str> for Decimal128 {
136  /// Returns a [Decimal128] initialized from string slice value.
137  fn from(s: &str) -> Self {
138    Self(DecQuad::from(s))
139  }
140}