Module malachite_base::num::conversion::string::to_sci
source · Expand description
ToSci
, a trait for converting a number to string, possibly using
scientific notation.
§to_sci
use malachite_base::num::conversion::traits::ToSci;
// If the value can fit in a `u32`, the result is the same as with `to_string`
assert_eq!(123u8.to_sci().to_string(), "123");
assert_eq!(u128::MAX.to_sci().to_string(), "3.402823669209385e38");
assert_eq!(i128::MIN.to_sci().to_string(), "-1.701411834604692e38");
§to_sci_with_options
use malachite_base::num::conversion::string::options::ToSciOptions;
use malachite_base::num::conversion::traits::ToSci;
use malachite_base::rounding_modes::RoundingMode::*;
let mut options = ToSciOptions::default();
assert_eq!(123456u32.to_sci_with_options(options).to_string(), "123456");
options.set_precision(3);
assert_eq!(123456u32.to_sci_with_options(options).to_string(), "1.23e5");
options.set_rounding_mode(Ceiling);
assert_eq!(123456u32.to_sci_with_options(options).to_string(), "1.24e5");
options.set_e_uppercase();
assert_eq!(123456u32.to_sci_with_options(options).to_string(), "1.24E5");
options.set_force_exponent_plus_sign(true);
assert_eq!(
123456u32.to_sci_with_options(options).to_string(),
"1.24E+5"
);
options = ToSciOptions::default();
options.set_base(36);
assert_eq!(123456u32.to_sci_with_options(options).to_string(), "2n9c");
options.set_uppercase();
assert_eq!(123456u32.to_sci_with_options(options).to_string(), "2N9C");
options.set_base(2);
options.set_precision(10);
assert_eq!(
123456u32.to_sci_with_options(options).to_string(),
"1.1110001e16"
);
options.set_include_trailing_zeros(true);
assert_eq!(
123456u32.to_sci_with_options(options).to_string(),
"1.111000100e16"
);
§fmt_sci_valid
use malachite_base::num::conversion::string::options::ToSciOptions;
use malachite_base::num::conversion::traits::ToSci;
use malachite_base::rounding_modes::RoundingMode::*;
let mut options = ToSciOptions::default();
assert!(123u8.fmt_sci_valid(options));
assert!(u128::MAX.fmt_sci_valid(options));
options.set_rounding_mode(Exact);
assert!(!u128::MAX.fmt_sci_valid(options)); // u128::MAX has more than 16 significant digits
options.set_precision(50);
assert!(u128::MAX.fmt_sci_valid(options));
Structs§
- A
struct
that can be used to format a number in scientific notation.