diff options
| author | SiegeLord <slabode@aim.com> | 2014-01-16 19:19:52 -0500 |
|---|---|---|
| committer | SiegeLord <slabode@aim.com> | 2014-01-22 20:32:40 -0500 |
| commit | 25b107f1e346cc6c365de8dd1b157d85a57df72e (patch) | |
| tree | 2345eb6d884fdcf3c4dfa12cd4f5bdad08ff4d93 /src/libstd/num | |
| parent | 2b4bd0780bb98f4171dd6464a4f01065b8d85ffd (diff) | |
Add LowerExp 'e' and UpperExp 'E' format traits/specifiers
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/f32.rs | 34 | ||||
| -rw-r--r-- | src/libstd/num/f64.rs | 34 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index e95ad7ca7f5..5df4b598a88 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -783,6 +783,40 @@ pub fn to_str_digits(num: f32, dig: uint) -> ~str { r } +/// +/// Converts a float to a string using the exponential notation with exactly the number of +/// provided digits after the decimal point in the significand +/// +/// # Arguments +/// +/// * num - The float value +/// * digits - The number of digits after the decimal point +/// * upper - Use `E` instead of `e` for the exponent sign +/// +#[inline] +pub fn to_str_exp_exact(num: f32, dig: uint, upper: bool) -> ~str { + let (r, _) = strconv::float_to_str_common( + num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper); + r +} + +/// +/// Converts a float to a string using the exponential notation with the maximum number of +/// digits after the decimal point in the significand +/// +/// # Arguments +/// +/// * num - The float value +/// * digits - The number of digits after the decimal point +/// * upper - Use `E` instead of `e` for the exponent sign +/// +#[inline] +pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> ~str { + let (r, _) = strconv::float_to_str_common( + num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper); + r +} + impl to_str::ToStr for f32 { #[inline] fn to_str(&self) -> ~str { to_str_digits(*self, 8) } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 33ee4acf58b..6c0a2a41ec1 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -785,6 +785,40 @@ pub fn to_str_digits(num: f64, dig: uint) -> ~str { r } +/// +/// Converts a float to a string using the exponential notation with exactly the number of +/// provided digits after the decimal point in the significand +/// +/// # Arguments +/// +/// * num - The float value +/// * digits - The number of digits after the decimal point +/// * upper - Use `E` instead of `e` for the exponent sign +/// +#[inline] +pub fn to_str_exp_exact(num: f64, dig: uint, upper: bool) -> ~str { + let (r, _) = strconv::float_to_str_common( + num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper); + r +} + +/// +/// Converts a float to a string using the exponential notation with the maximum number of +/// digits after the decimal point in the significand +/// +/// # Arguments +/// +/// * num - The float value +/// * digits - The number of digits after the decimal point +/// * upper - Use `E` instead of `e` for the exponent sign +/// +#[inline] +pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> ~str { + let (r, _) = strconv::float_to_str_common( + num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper); + r +} + impl to_str::ToStr for f64 { #[inline] fn to_str(&self) -> ~str { to_str_digits(*self, 8) } |
