/// Construct an [`Decimal`] from an float, with a given power of 10 for the lower magnitude #[diplomat::rust_link(fixed_decimal::Decimal::try_from_f64, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)] #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)] #[diplomat::attr(js, rename = "from_number_with_lower_magnitude")] #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)] pubfn from_double_with_lower_magnitude(
f: f64,
magnitude: i16,
) -> Result<Box<Decimal>, DecimalLimitError> { let precision = fixed_decimal::DoublePrecision::Magnitude(magnitude);
Ok(Box::new(Decimal(fixed_decimal::Decimal::try_from_f64(
f, precision,
)?)))
}
/// Construct an [`Decimal`] from an float, for a given number of significant digits #[diplomat::rust_link(fixed_decimal::Decimal::try_from_f64, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)] #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)] #[diplomat::attr(js, rename = "from_number_with_significant_digits")] #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)] pubfn from_double_with_significant_digits(
f: f64,
digits: u8,
) -> Result<Box<Decimal>, DecimalLimitError> { let precision = fixed_decimal::DoublePrecision::SignificantDigits(digits);
Ok(Box::new(Decimal(fixed_decimal::Decimal::try_from_f64(
f, precision,
)?)))
}
/// Construct an [`Decimal`] from an float, with enough digits to recover /// the original floating point in IEEE 754 without needing trailing zeros #[diplomat::rust_link(fixed_decimal::Decimal::try_from_f64, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::FloatPrecision, Enum)] #[diplomat::rust_link(fixed_decimal::DoublePrecision, Enum, hidden)] #[diplomat::attr(js, rename = "from_number_with_round_trip_precision")] #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)] pubfn from_double_with_round_trip_precision(
f: f64,
) -> Result<Box<Decimal>, DecimalLimitError> { let precision = fixed_decimal::DoublePrecision::RoundTrip;
Ok(Box::new(Decimal(fixed_decimal::Decimal::try_from_f64(
f, precision,
)?)))
}
/// Multiply the [`Decimal`] by a given power of ten. #[diplomat::rust_link(fixed_decimal::Decimal::multiply_pow10, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::multiplied_pow10, FnInTypedef, hidden)] pubfn multiply_pow10(&mutself, power: i16) { self.0.multiply_pow10(power)
}
/// Zero-pad the [`Decimal`] on the left to a particular position #[diplomat::rust_link(fixed_decimal::Decimal::pad_start, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::padded_start, FnInTypedef, hidden)] pubfn pad_start(&mutself, position: i16) { self.0.absolute.pad_start(position)
}
/// Zero-pad the [`Decimal`] on the right to a particular position #[diplomat::rust_link(fixed_decimal::Decimal::pad_end, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::padded_end, FnInTypedef, hidden)] pubfn pad_end(&mutself, position: i16) { self.0.absolute.pad_end(position)
}
/// Truncate the [`Decimal`] on the left to a particular position, deleting digits if necessary. This is useful for, e.g. abbreviating years /// ("2022" -> "22") #[diplomat::rust_link(fixed_decimal::Decimal::set_max_position, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::with_max_position, FnInTypedef, hidden)] pubfn set_max_position(&mutself, position: i16) { self.0.absolute.set_max_position(position)
}
/// Round the number at a particular digit position. /// /// This uses half to even rounding, which resolves ties by selecting the nearest /// even integer to the original value. #[diplomat::rust_link(fixed_decimal::Decimal::round, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::rounded, FnInTypedef, hidden)] pubfn round(&mutself, position: i16) { self.0.round(position)
}
/// Concatenates `other` to the end of `self`. /// /// If successful, `other` will be set to 0 and a successful status is returned. /// /// If not successful, `other` will be unchanged and an error is returned. #[diplomat::rust_link(fixed_decimal::Decimal::concatenate_end, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::concatenated_end, FnInTypedef, hidden)] pubfn concatenate_end(&mutself, other: &>mut Decimal) -> Result<(), ()> { let x = core::mem::take(&mut other.0); self.0.absolute.concatenate_end(x.absolute).map_err(|y| {
other.0.absolute = y;
})
}
/// Format the [`Decimal`] as a string. #[diplomat::rust_link(fixed_decimal::Decimal::write_to, FnInTypedef)] #[diplomat::rust_link(fixed_decimal::Decimal::to_string, FnInTypedef, hidden)] #[diplomat::attr(auto, stringifier)] #[diplomat::attr(demo_gen, disable)] // this just returns the single constructor argument pubfn to_string(&self, to: &mut diplomat_runtime::DiplomatWrite) { let _ = self.0.write_to(to);
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.