Skip to content

Commit

Permalink
Merge pull request #1302 from quadratichq/float-issues-in-formulas
Browse files Browse the repository at this point in the history
Scale round floating point numbers for display
  • Loading branch information
davidkircos authored Apr 23, 2024
2 parents af2dc25 + 9fde8ac commit e222207
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions quadratic-core/src/grid/sheet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{btree_map, BTreeMap};
use std::str::FromStr;

use bigdecimal::BigDecimal;
use bigdecimal::{BigDecimal, RoundingMode};
use indexmap::IndexMap;
use rand::Rng;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -342,15 +342,19 @@ impl Sheet {
if let Some(value) = self.display_value(pos) {
match value {
CellValue::Number(n) => {
let (_, exponent) = n.as_bigint_and_exponent();
let exponent = n.as_bigint_and_exponent().1;
let max_decimals = 9;
let decimals = exponent.min(max_decimals) as i16;
let mut decimals = n
.with_scale_round(exponent.min(max_decimals), RoundingMode::HalfUp)
.normalized()
.as_bigint_and_exponent()
.1 as i16;

if is_percentage {
Some(decimals - 2)
} else {
Some(decimals)
decimals -= 2;
}

Some(decimals)
}
_ => None,
}
Expand Down Expand Up @@ -453,6 +457,19 @@ mod test {
assert_eq!(sheet.decimal_places(Pos { x: 1, y: 2 }, false), None);
}

#[test]
fn test_current_decimal_places_float() {
let mut sheet = Sheet::new(SheetId::new(), String::from(""), String::from(""));

sheet.set_cell_value(
crate::Pos { x: 1, y: 2 },
CellValue::Number(BigDecimal::from_str("11.100000000000000000").unwrap()),
);

// expect a single decimal place
assert_eq!(sheet.decimal_places(Pos { x: 1, y: 2 }, false), Some(1));
}

#[test]
fn test_cell_numeric_format_kind() {
let mut sheet = Sheet::new(SheetId::new(), String::from(""), String::from(""));
Expand Down

0 comments on commit e222207

Please sign in to comment.