pub fn si_number<T>(s: &str) -> Result<T, String>where
<T as TryFrom<u128>>::Error: Display,
<T as FromStr>::Err: Display,
T: CheckedAdd + CheckedMul + CheckedSub + FromStr + PartialOrd + TryFrom<u128> + Zero,Expand description
Validate a signed or unsigned integer value with a metric prefix.
This can accept strings with the (case sensitive) SI symbols.
| Symbol | Name | Value |
|---|---|---|
| Y | yotta | 1_000_000_000_000_000_000_000_000 |
| Z | zetta | 1_000_000_000_000_000_000_000 |
| E | exa | 1_000_000_000_000_000_000 |
| P | peta | 1_000_000_000_000_000 |
| T | tera | 1_000_000_000_000 |
| G | giga | 1_000_000_000 |
| M | mega | 1_000_000 |
| k | kilo | 1_000 |
The strings can be provided with a decimal, or using the SI symbol as the decimal separator.
| String | Value |
|---|---|
| 3k3 | 3300 |
| 3.3k | 3300 |
| 1M | 1_000_000 |
ยงExample
This allows for resistance value to be passed using SI symbols.
use clap::Parser;
use clap_num::si_number;
#[derive(Parser)]
struct Args {
#[clap(short, long, value_parser=si_number::<u128>)]
resistance: u128,
}To run this example use cargo run --example resistance, giving arguments
to the program after --, for example:
$ cargo run --example resistance -- --resistance 1M1
Resistance: 1100000 ohms