Skip to content

Commit

Permalink
(Issue rust-num#10) Fixed things pointed out by cuviper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazardius committed Oct 6, 2018
1 parent 69be761 commit 26a3e6a
Showing 1 changed file with 31 additions and 41 deletions.
72 changes: 31 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,6 @@ impl<T: Clone + Integer + Signed> Signed for Ratio<T> {
}
}

#[cfg(feature = "std")]
use std::string::String;

#[cfg(feature = "std")]
trait AsDecimal {
fn as_decimal(&self, usize) -> String;
}

// String conversions
impl<T> fmt::Display for Ratio<T>
where
Expand All @@ -1002,44 +994,43 @@ where
}

#[cfg(feature = "std")]
impl<T> AsDecimal for Ratio<T> where
use std::string::String;

#[cfg(feature = "std")]
impl<T> Ratio<T> where
T: fmt::Display
+ Clone
+ Eq
+ FromPrimitive
+ Integer
+ Pow<usize, Output = T>
+ Signed
+ std::string::ToString,
{
/// Generates decimal approximation to a specified precision as a String.
fn as_decimal(&self, precision: usize) -> String {
if self.denom.is_one() {
self.numer.to_string()
pub fn as_decimal(&self, precision: usize) -> String {
use traits::pow;
use std::string::ToString;
let ten_pow_prec = pow(T::from_usize(10).unwrap(), precision);
let rounded = (self.clone() * &ten_pow_prec).round();
let minus = rounded < Zero::zero();
let trunc = &((&rounded / &ten_pow_prec).trunc().to_integer());
let tail = &(rounded % ten_pow_prec).abs().to_integer();
let mut ret_val = String::from("");
if tail.is_zero() {
ret_val.push_str(&trunc.to_string());
} else {
let _10_pow_prec = T::from_usize(10).unwrap().pow(precision);
let rounded = (self.clone() * &_10_pow_prec).round();
let minus = rounded < Zero::zero();
let trunc = &((&rounded / &_10_pow_prec).trunc().to_integer());
let tail = &(rounded % _10_pow_prec).abs().to_integer();
if tail.is_zero() {
trunc.to_string()
} else {
let mut ret_val = String::from(if minus { "-" } else { "" });
ret_val.push_str(&trunc.to_string());
ret_val.push_str(".");
let tail_strarr = &tail.to_string();
let tail_length = &tail_strarr.chars().count();
let tail_zeroes = if tail_length < &precision {
// If rust >= 1.16.0 - we can simply use repeat.
// "0".repeat(precision - tail_length)
(0..(precision - tail_length)).map(|_| "0").collect::<String>()
} else { String::from("") };
ret_val.push_str(&tail_zeroes);
ret_val.push_str(&tail.to_string());
ret_val
}
}
if minus { ret_val.push_str("-") };
ret_val.push_str(&trunc.to_string());
}
ret_val.push_str(".");
let tail_strarr = &tail.to_string();
let tail_length = &tail_strarr.chars().count();
let tail_zeroes = if *tail_length < precision {
// If rust >= 1.16.0 - we can simply use repeat.
// "0".repeat(precision - tail_length)
(0..(precision - tail_length)).map(|_| "0").collect::<String>()
} else { String::from("") };
ret_val.push_str(&tail_zeroes);
ret_val.push_str(&tail.to_string());
ret_val
}
}

Expand Down Expand Up @@ -1602,17 +1593,16 @@ mod test {
#[test]
#[cfg(feature = "std")]
fn test_as_decimal() {
use AsDecimal;
use std::string::ToString;
assert_eq!(_2.as_decimal(2), "2".to_string());
assert_eq!(_2.as_decimal(2), "2.00".to_string());
assert_eq!(_1_2.as_decimal(2), "0.50".to_string());
assert_eq!(_3_2.as_decimal(1), "1.5".to_string());
assert_eq!(_NEG1_2.as_decimal(2), "-0.50".to_string());
assert_eq!(_NEG2_3.as_decimal(3), "-0.667".to_string());
assert_eq!(_1_8.as_decimal(2), "0.13".to_string());
assert_eq!(_NEG1_8.as_decimal(4), "-0.1250".to_string());
assert_eq!(_99999_1000.as_decimal(3), "99.999".to_string());
assert_eq!(_NEG99999_1000.as_decimal(1), "-100".to_string());
assert_eq!(_NEG99999_1000.as_decimal(1), "-100.0".to_string());
}

mod arith {
Expand Down

0 comments on commit 26a3e6a

Please sign in to comment.