mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-08 06:28:56 +01:00
Fixed formatting of scientific notation, closes #81
This commit is contained in:
parent
39673bc647
commit
5f40e7202e
@ -53,7 +53,7 @@ lazy_static! {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ScientificNotation {
|
pub struct ScientificNotation {
|
||||||
pub negative: bool,
|
pub negative: bool,
|
||||||
pub(crate) digits: String,
|
pub value: f64,
|
||||||
pub exponent: i32,
|
pub exponent: i32,
|
||||||
pub imaginary: bool,
|
pub imaginary: bool,
|
||||||
}
|
}
|
||||||
@ -69,21 +69,13 @@ pub enum ComplexNumberType {
|
|||||||
impl ScientificNotation {
|
impl ScientificNotation {
|
||||||
#[wasm_bindgen(js_name = toString)]
|
#[wasm_bindgen(js_name = toString)]
|
||||||
pub fn to_string(&self) -> String {
|
pub fn to_string(&self) -> String {
|
||||||
if self.digits == "" {
|
|
||||||
return String::from("0");
|
|
||||||
}
|
|
||||||
|
|
||||||
let sign = if self.negative { "-" } else { "" };
|
let sign = if self.negative { "-" } else { "" };
|
||||||
let mut digits_and_mul = if self.digits == "1" {
|
let digits_and_mul = if self.value == 1f64 {
|
||||||
String::new()
|
String::new()
|
||||||
} else {
|
} else {
|
||||||
format!("{}×", &self.digits)
|
format!("{}×", format_number(self.value))
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.digits.len() > 1 {
|
|
||||||
digits_and_mul.insert(1usize, '.');
|
|
||||||
}
|
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"{}{}10^{} {}",
|
"{}{}10^{} {}",
|
||||||
sign,
|
sign,
|
||||||
@ -107,31 +99,17 @@ impl KalkNum {
|
|||||||
&self,
|
&self,
|
||||||
complex_number_type: ComplexNumberType,
|
complex_number_type: ComplexNumberType,
|
||||||
) -> ScientificNotation {
|
) -> ScientificNotation {
|
||||||
let value_string = match complex_number_type {
|
|
||||||
ComplexNumberType::Real => self.to_string_real(10),
|
|
||||||
ComplexNumberType::Imaginary => self.to_string_imaginary(10, false),
|
|
||||||
}
|
|
||||||
.trim_start_matches("-")
|
|
||||||
.to_string();
|
|
||||||
let trimmed = if value_string.contains(".") {
|
|
||||||
value_string.trim_end_matches("0")
|
|
||||||
} else {
|
|
||||||
&value_string
|
|
||||||
};
|
|
||||||
let value = match complex_number_type {
|
let value = match complex_number_type {
|
||||||
ComplexNumberType::Real => &self.value,
|
ComplexNumberType::Real => self.to_f64(),
|
||||||
ComplexNumberType::Imaginary => &self.imaginary_value,
|
ComplexNumberType::Imaginary => self.imaginary_to_f64(),
|
||||||
};
|
};
|
||||||
|
let exponent = value.clone().abs().log10().floor() as i32 + 1;
|
||||||
|
|
||||||
ScientificNotation {
|
ScientificNotation {
|
||||||
negative: value < &0f64,
|
negative: value < 0f64,
|
||||||
digits: trimmed
|
value: value / (10f64.pow(exponent - 1) as f64),
|
||||||
.to_string()
|
|
||||||
.replace(".", "")
|
|
||||||
.trim_start_matches("0")
|
|
||||||
.to_string(),
|
|
||||||
// I... am not sure what else to do...
|
// I... am not sure what else to do...
|
||||||
exponent: KalkNum::new(value.clone().abs().log10() + 1f64, "").to_i32(),
|
exponent,
|
||||||
imaginary: complex_number_type == ComplexNumberType::Imaginary,
|
imaginary: complex_number_type == ComplexNumberType::Imaginary,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user