0.1.0 |
|
---|
#41 in #string-representation
185KB
3.5K SLoC
flt2dec2flt
该包提供将浮点数(f32 和
f64
)转换为十进制字符串以及反向转换的低级别函数。
许可证
该项目受以下任一许可证的许可:
- Apache 许可证 2.0 版,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证(LICENSE-MIT 或 http://opensource.org/licenses/MIT)
由您选择。
lib.rs
:
该包提供将浮点数(f32 和
f64
)转换为十进制字符串以及反向转换的低级别函数。
实现精确的浮点数字符串转换并非易事(例如,请参阅 Marc Andrysco、Ranjit Jhala 和 Sorin Lerner 的 "打印浮点数:一种始终正确的方法")。Rust 标准库内部使用复杂的算法(位于 libcore/num
)来完成此目的,但它们未公开,只能通过 FromStr::from_str
、ToString::to_string
或 Display::fmt
来使用。
这些函数对浮点数的字符串表示形式施加了格式,这可能并不总是适合您。例如,您可能希望写入 1.2 * 10^4
而不是 1.2e4
。
该包公开了核心算法,允许您在不担心复杂数学部分的情况下实现自定义的浮点数字符串转换。
此包的功能通过 FloatExt
trait 提供,该 trait 为 f32
和 f64
实现。
最低 Rust 版本
此包所需的最低 Rust 版本是 1.34。
示例(浮点数转换为字符串)
// Let's say we want to convert this value to a string
// in exponential form.
let value = 1.25e20;
// Using the standard library, you can use `format!("{:e}")`:
assert_eq!(format!("{:e}", value), "1.25e20");
// It also allows tu use a capital 'E':
assert_eq!(format!("{:E}", value), "1.25E20");
// or to include an explicit '+':
assert_eq!(format!("{:+e}", value), "+1.25e20");
// or to used a fixed number of digits:
assert_eq!(format!("{:.04e}", value), "1.2500e20");
// However, besides those options, `format!` imposes the
// format of the string representation of the floating point
// number (using `.` as decimal separator and `e` or `E` as
// exponential separator).
// This crate provides low-level functions to conver floating point
// numbers to strings without an imposed format.
use flt2dec2flt::FloatExt as _;
// The `FloatExt::preformat_*` functions pre-converts the floating
// point numbers into string, providing a decomposed intermediate
// result.
let mut buf = [0; flt2dec2flt::PREFORMAT_SHORTEST_BUF_LEN];
// You could also use `f32::preformat_shortest(value, &mut buf)`
let preformatted = value.preformat_shortest(&mut buf);
// `false` means the the number is positive, `b"125"` are the
// significant digits and `21` is the exponent (such as
// `1.25e20 == 0.125e21`)
assert_eq!(preformatted, flt2dec2flt::PreFormatted::Finite(false, b"125", 21));
// From this decomposed form, you can now build your custom string
// representation of the floating point number.
示例(字符串转换为浮点数)
use std::str::FromStr as _;
// Let's say you want to convert a string to a floating
// point number.
// Using the standard library, you can use `FromStr::from_str`:
assert_eq!(f32::from_str("1.25e20").unwrap(), 1.25e20);
// However, this function imposes the format of the input string.
// This crate provides functions to convert a pre-parsed string to
// a floating.
use flt2dec2flt::FloatExt as _;
// You have to implement your pre-parsing based on your string format.
// So, `1.25e20` (or `1.25*10^20`) would be pre-parsed as:
let preparsed = flt2dec2flt::PreParsed {
// positive
sign: false,
// digits of the integer part
int_digits: b"1",
// digits of the fractional part
frac_digits: b"25",
// exponent
exp: 20,
};
// Which can be converted to a floating point number:
assert_eq!(f32::from_preparsed(preparsed).unwrap(), 1.25e20);