feat: don't print scientific notation for n < 1000

unfortunately it isn't this easy for n > 0.001,
a similar strategy seems to prefer printing eg 1.0 * 10^-1
This commit is contained in:
electria 2026-10-02 13:22:49 -07:00
commit c745d6b5d2
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -19,10 +19,19 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
.is_some(); .is_some();
let result = nodes::eval(&input)?; let result = nodes::eval(&input)?;
let significant_digits = get_significant_digits(&result).min(MAX_DIGITS); let display_digits = if result >= 1000 || result < 1 {
get_significant_digits(&result).min(MAX_DIGITS)
} else if result >= 100 {
3
} else if result >= 10 {
2
// if result >= 1
} else {
1
};
Ok(format!( Ok(format!(
"{}{}{result:.significant_digits$}{}", "{}{}{result:.display_digits$}{}",
" ".repeat(leading_spaces), " ".repeat(leading_spaces),
if should_add_back_equals && should_add_back_alignment { if should_add_back_equals && should_add_back_alignment {
"& = " "& = "
@ -44,6 +53,14 @@ fn test_character_readdition() {
assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11"); assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11");
assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\"); assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\");
} }
#[test]
fn test_basic_up_to_999() {
assert_eq!(pretty_eval("1".into()).unwrap(), "1");
assert_eq!(pretty_eval("10".into()).unwrap(), "10");
assert_eq!(pretty_eval("100".into()).unwrap(), "100");
assert_eq!(pretty_eval("999".into()).unwrap(), "999");
assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3");
}
pub fn get_significant_digits(n: &Float) -> usize { pub fn get_significant_digits(n: &Float) -> usize {
let raw_string = n.to_string().replace('.', ""); let raw_string = n.to_string().replace('.', "");