Fixed output not displaying small-ish decimal numbers properly.

This commit is contained in:
PaddiM8 2020-06-06 17:20:40 +02:00
parent c2e446576a
commit 2074a5b15b
2 changed files with 6 additions and 8 deletions

View File

@ -134,11 +134,11 @@ pub mod special_funcs {
} }
pub fn to_degrees(x: Float) -> Float { pub fn to_degrees(x: Float) -> Float {
Float::with_val(10, x.to_f64().to_degrees()) Float::with_val(53, x.to_f64().to_degrees())
} }
pub fn to_radians(x: Float) -> Float { pub fn to_radians(x: Float) -> Float {
Float::with_val(10, x.to_f64().to_radians()) Float::with_val(53, x.to_f64().to_radians())
} }
} }

View File

@ -9,21 +9,19 @@ pub fn eval(parser: &mut parser::Context, input: &str) {
if result.is_infinite() { if result.is_infinite() {
print_err("Too big to process."); print_err("Too big to process.");
/*} else if result.clone().fract() == 0 {
println!("{}", result.to_integer().unwrap());*/
} else { } else {
let use_sci_notation = exp > 8 || exp < -6; let use_sci_notation = exp > 8 || exp < -6;
let comma_pos = if use_sci_notation { 1 } else { exp as usize }; let comma_pos = if use_sci_notation { 1 } else { exp as usize };
let sign = if result >= 0 { "" } else { "-" }; let sign = if result >= 0 { "" } else { "-" };
let num = if use_sci_notation { let num = if exp <= 0 {
// 0 < x < 1
format!("0.{}{}", "0".repeat(exp.abs() as usize), digits)
} else if use_sci_notation || result.fract() != 0 {
// Insert the comma if there are supposed to be decimals. // Insert the comma if there are supposed to be decimals.
let mut chars: Vec<char> = digits.trim_end_matches('0').chars().collect(); let mut chars: Vec<char> = digits.trim_end_matches('0').chars().collect();
chars.insert(comma_pos, '.'); chars.insert(comma_pos, '.');
chars.into_iter().collect::<String>() chars.into_iter().collect::<String>()
} else if exp < 0 {
// 0 < x < 1
format!("0.{}{}", "0".repeat(exp.abs() as usize), digits)
} else { } else {
// Regular number // Regular number
digits[..(exp as usize)].to_string() digits[..(exp as usize)].to_string()