GFFixedDecimal¶
API Reference / Standard / 类索引
- 路径:
addons/gf/standard/foundation/numeric/gf_fixed_decimal.gd - 模块:
Standard - 继承:
RefCounted - API:
public - 类别:值对象 (
value_object) - 首次版本:
3.17.0
基于整数缩放的定点小数值对象。 适合处理货币、税率、经营数值等对“累计误差”敏感、 但又不需要无限精度十进制库的场景。
成员概览¶
| 类型 | 名称 | 签名 |
|---|---|---|
| 枚举 | RoundingMode |
enum RoundingMode |
| 常量 | MAX_DECIMAL_PLACES |
const MAX_DECIMAL_PLACES: int = 18 |
| 属性 | raw_value |
var raw_value: int = 0 |
| 属性 | decimal_places |
var decimal_places: int = 2 |
| 方法 | from_int |
static func from_int(value: int, p_decimal_places: int = 2) -> GFFixedDecimal: |
| 方法 | from_float |
static func from_float( value: float, p_decimal_places: int = 2, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal: |
| 方法 | from_string |
static func from_string( value: String, p_decimal_places: int = 2, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal: |
| 方法 | clone |
func clone() -> GFFixedDecimal: |
| 方法 | is_zero |
func is_zero() -> bool: |
| 方法 | abs_value |
func abs_value() -> GFFixedDecimal: |
| 方法 | negated |
func negated() -> GFFixedDecimal: |
| 方法 | rescaled |
func rescaled( target_decimal_places: int, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal: |
| 方法 | compare_to |
func compare_to(other: GFFixedDecimal) -> int: |
| 方法 | add |
func add(other: GFFixedDecimal) -> GFFixedDecimal: |
| 方法 | subtract |
func subtract(other: GFFixedDecimal) -> GFFixedDecimal: |
| 方法 | multiply |
func multiply( other: GFFixedDecimal, target_decimal_places: int = -1, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal: |
| 方法 | divide |
func divide( other: GFFixedDecimal, target_decimal_places: int = -1, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal: |
| 方法 | to_float |
func to_float() -> float: |
| 方法 | to_big_number |
func to_big_number() -> GFBigNumber: |
| 方法 | to_decimal_string |
func to_decimal_string(trim_zeroes: bool = false) -> String: |
枚举¶
RoundingMode¶
- API:
public
enum RoundingMode { ## 四舍五入,0.5 始终朝绝对值更大的方向进位。 HALF_UP, ## 银行家舍入,0.5 时向最近的偶数靠拢。 HALF_EVEN, ## 向负无穷方向取整。 FLOOR, ## 向正无穷方向取整。 CEIL, ## 直接截断,朝 0 逼近。 TRUNCATE, }
缩放或除法时使用的舍入策略。
常量¶
MAX_DECIMAL_PLACES¶
- API:
public
定点数可保留的小数位上限,避免整数缩放时溢出。
属性¶
raw_value¶
- API:
public
实际保存的整数值。
decimal_places¶
- API:
public
小数位数。
方法¶
from_int¶
- API:
public
从 int 构建定点数。
参数:
| 名称 | 说明 |
|---|---|
value |
原始整数。 |
p_decimal_places |
目标小数位。 |
返回:定点数实例。
from_float¶
- API:
public
static func from_float( value: float, p_decimal_places: int = 2, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal:
从 float 构建定点数。
参数:
| 名称 | 说明 |
|---|---|
value |
原始浮点数。 |
p_decimal_places |
目标小数位。 |
rounding_mode |
舍入策略。 |
返回:定点数实例。
from_string¶
- API:
public
static func from_string( value: String, p_decimal_places: int = 2, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal:
从字符串构建定点数。
参数:
| 名称 | 说明 |
|---|---|
value |
普通十进制字符串。 |
p_decimal_places |
目标小数位。 |
rounding_mode |
舍入策略。 |
返回:定点数实例。
clone¶
- API:
public
克隆当前定点数。
返回:内容相同的新实例。
is_zero¶
- API:
public
当前值是否为零。
返回:为零时返回 true。
abs_value¶
- API:
public
获取绝对值。
返回:新的定点数实例。
negated¶
- API:
public
获取相反数。
返回:新的定点数实例。
rescaled¶
- API:
public
func rescaled( target_decimal_places: int, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal:
重设小数位数。
参数:
| 名称 | 说明 |
|---|---|
target_decimal_places |
目标小数位数。 |
rounding_mode |
降位时的舍入策略。 |
返回:重设后的定点数实例。
compare_to¶
- API:
public
与另一个定点数比较大小。
参数:
| 名称 | 说明 |
|---|---|
other |
另一个定点数。 |
返回:大于返回 1,小于返回 -1,相等返回 0。
add¶
- API:
public
与另一个定点数相加。
参数:
| 名称 | 说明 |
|---|---|
other |
另一个定点数。 |
返回:相加结果。
subtract¶
- API:
public
与另一个定点数相减。
参数:
| 名称 | 说明 |
|---|---|
other |
另一个定点数。 |
返回:相减结果。
multiply¶
- API:
public
func multiply( other: GFFixedDecimal, target_decimal_places: int = -1, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal:
与另一个定点数相乘。
参数:
| 名称 | 说明 |
|---|---|
other |
另一个定点数。 |
target_decimal_places |
结果小数位;传 -1 时取两者较大值。 |
rounding_mode |
结果降位时的舍入策略。 |
返回:相乘结果。
divide¶
- API:
public
func divide( other: GFFixedDecimal, target_decimal_places: int = -1, rounding_mode: RoundingMode = RoundingMode.HALF_UP ) -> GFFixedDecimal:
与另一个定点数相除。
参数:
| 名称 | 说明 |
|---|---|
other |
另一个定点数。 |
target_decimal_places |
结果小数位;传 -1 时取两者较大值。 |
rounding_mode |
除法舍入策略。 |
返回:相除结果。
to_float¶
- API:
public
转换为 float。
返回:浮点值。
to_big_number¶
- API:
public
转换为 GFBigNumber。
返回:对应的大数值对象。
to_decimal_string¶
- API:
public
转换为普通字符串。
参数:
| 名称 | 说明 |
|---|---|
trim_zeroes |
是否裁掉尾部 0。 |
返回:十进制字符串。