| Crates.io | num-rational-parse |
| lib.rs | num-rational-parse |
| version | 0.1.0 |
| created_at | 2025-12-25 23:08:18.274971+00 |
| updated_at | 2025-12-25 23:08:18.274971+00 |
| description | A flexible string parsing extension for num_rational::Ratio, supporting fractions, decimals, and scientific notation (inspired by Python's fractions module). |
| homepage | |
| repository | https://github.com/Boslx/num-rational-parse |
| max_upload_size | |
| id | 2005034 |
| size | 38,598 |
Extension for num_rational providing flexible string parsing for Ratio<T> types.
It allows you to parse:
"3/4""1.25""1.2e-3", "1E5"Add this to your Cargo.toml:
[dependencies]
num-rational = "0.4"
num-rational-parse = "0.1"
Then import the RationalParse trait:
use num_rational::Ratio;
use num_rational_parse::RationalParse;
fn main() {
let r = Ratio::<i32>::from_str_flex("3.14").unwrap();
println!("{}", r); // Prints "157/50"
let r2 = Ratio::<i32>::from_str_flex("1.2e-2").unwrap();
println!("{}", r2); // Prints "3/250"
}
The standard from_str implementation in num_rational only supports the numerator/denominator format. num-rational-parse extends this to support decimals and scientific notation.
Unlike parsing to a floating-point number (like f64) and then converting to a fraction, num-rational-parse parses decimal strings directly into their exact rational representation. This avoids precision loss or rounding errors commonly associated with floating-point math.
Licensed under either of
at your option.
The parsing approach is inspired by Python®'s fractions module.
"Python" is a registered trademark of the Python Software Foundation.