1 unstable release

0.1.0 Apr 1, 2019

#77 in #number

Download history 8/week @ 2024-08-26 7/week @ 2024-09-02 34/week @ 2024-09-09 56/week @ 2024-09-16 32/week @ 2024-09-23 18/week @ 2024-09-30 27/week @ 2024-10-07 1/week @ 2024-10-14 44/week @ 2024-11-04 105/week @ 2024-11-11 40/week @ 2024-11-18 66/week @ 2024-11-25 19/week @ 2024-12-09

127 downloads per month
Used in 2 crates

MIT license

16KB
251 lines

numberkit

Build Status

Number kit for Rust.

fast string to number operation.

Usage

[dependencies]
numberkit = "0.1"

Check string is a number

assert_eq!(true, numberkit::is_number("0"));
assert_eq!(false, numberkit::is_number("-1u32"));
assert_eq!(true, numberkit::is_number("2usize"));
assert_eq!(true, numberkit::is_number("3.5f32"));
assert_eq!(false, numberkit::is_number("0.2.1f32"));

support isize usize float string check.

In addition, support digit check.

assert_eq!(true, numberkit::is_digit("2", false));
assert_eq!(false, numberkit::is_digit("-2", false));
assert_eq!(false, numberkit::is_digit("0.2", false));
assert_eq!(true, numberkit::is_idigit("-2"));
assert_eq!(true, numberkit::is_udigit("2"));

Covert string to number

assert_eq!(1 as isize, numberkit::as_isize("1").unwrap());
assert_eq!(-1 as isize, numberkit::as_isized("a", -1 as isize));
assert_eq!(1 as usize, numberkit::as_usize("1").unwrap());
assert_eq!(52 as i128, numberkit::as_i128("52").unwrap());
assert_eq!(0.5 as f64, numberkit::as_f64("0.5").unwrap());
assert_eq!(0.5 as f64, numberkit::as_f64d("0.5", 0.5 as f64));

as_* function will covert string to number, but return value this Result, because errors may occur.

as_*d function checks for errors and needs to provide a default value, using the default value when the send conversion fails.

No runtime deps