about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorSiegeLord <slabode@aim.com>2014-01-16 19:19:52 -0500
committerSiegeLord <slabode@aim.com>2014-01-22 20:32:40 -0500
commit25b107f1e346cc6c365de8dd1b157d85a57df72e (patch)
tree2345eb6d884fdcf3c4dfa12cd4f5bdad08ff4d93 /src/libstd
parent2b4bd0780bb98f4171dd6464a4f01065b8d85ffd (diff)
downloadrust-25b107f1e346cc6c365de8dd1b157d85a57df72e.tar.gz
rust-25b107f1e346cc6c365de8dd1b157d85a57df72e.zip
Add LowerExp 'e' and UpperExp 'E' format traits/specifiers
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/fmt/mod.rs30
-rw-r--r--src/libstd/num/f32.rs34
-rw-r--r--src/libstd/num/f64.rs34
3 files changed, 98 insertions, 0 deletions
diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs
index 411f9f25459..5b2a792a05b 100644
--- a/src/libstd/fmt/mod.rs
+++ b/src/libstd/fmt/mod.rs
@@ -147,6 +147,8 @@ The current mapping of types to traits is:
 * `p` ⇒ `Pointer`
 * `t` ⇒ `Binary`
 * `f` ⇒ `Float`
+* `e` ⇒ `LowerExp`
+* `E` ⇒ `UpperExp`
 * *nothing* ⇒ `Default`
 
 What this means is that any type of argument which implements the
@@ -578,6 +580,12 @@ pub trait Pointer { fn fmt(&Self, &mut Formatter); }
 /// Format trait for the `f` character
 #[allow(missing_doc)]
 pub trait Float { fn fmt(&Self, &mut Formatter); }
+/// Format trait for the `e` character
+#[allow(missing_doc)]
+pub trait LowerExp { fn fmt(&Self, &mut Formatter); }
+/// Format trait for the `E` character
+#[allow(missing_doc)]
+pub trait UpperExp { fn fmt(&Self, &mut Formatter); }
 
 /// The `write` function takes an output stream, a precompiled format string,
 /// and a list of arguments. The arguments will be formatted according to the
@@ -1085,6 +1093,28 @@ macro_rules! floating(($ty:ident) => {
             fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
         }
     }
+
+    impl LowerExp for $ty {
+        fn fmt(f: &$ty, fmt: &mut Formatter) {
+            // XXX: this shouldn't perform an allocation
+            let s = match fmt.precision {
+                Some(i) => ::$ty::to_str_exp_exact(f.abs(), i, false),
+                None => ::$ty::to_str_exp_digits(f.abs(), 6, false)
+            };
+            fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
+        }
+    }
+
+    impl UpperExp for $ty {
+        fn fmt(f: &$ty, fmt: &mut Formatter) {
+            // XXX: this shouldn't perform an allocation
+            let s = match fmt.precision {
+                Some(i) => ::$ty::to_str_exp_exact(f.abs(), i, true),
+                None => ::$ty::to_str_exp_digits(f.abs(), 6, true)
+            };
+            fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
+        }
+    }
 })
 floating!(f32)
 floating!(f64)
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) }