Skip to content

Commit c46b09f

Browse files
authored
feat: support scientific notation and decimal separator (#1688)
1 parent 91efcaf commit c46b09f

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,27 @@ impl NumberTypeOptionPB {
100100
pub(crate) fn format_cell_data(&self, s: &str) -> FlowyResult<NumberCellData> {
101101
match self.format {
102102
NumberFormat::Num => {
103-
let strnum = NUM_REGEX.replace_all(s, "");
104-
match Decimal::from_str(&strnum) {
105-
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
106-
Err(_) => Ok(NumberCellData::new()),
103+
if SCIENTIFIC_NOTATION_REGEX.is_match(s).unwrap() {
104+
match Decimal::from_scientific(&s.to_lowercase()) {
105+
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
106+
Err(_) => Ok(NumberCellData::new()),
107+
}
108+
} else {
109+
let draw_numer_string = NUM_REGEX.replace_all(s, "");
110+
let strnum = match draw_numer_string.matches(".").count() {
111+
0 | 1 => draw_numer_string.to_string(),
112+
_ => match EXTRACT_NUM_REGEX.captures(&draw_numer_string) {
113+
Ok(captures) => match captures {
114+
Some(capture) => capture[1].to_string(),
115+
None => "".to_string(),
116+
},
117+
Err(_) => "".to_string(),
118+
},
119+
};
120+
match Decimal::from_str(&strnum) {
121+
Ok(value, ..) => Ok(NumberCellData::from_decimal(value)),
122+
Err(_) => Ok(NumberCellData::new()),
123+
}
107124
}
108125
}
109126
_ => NumberCellData::from_format_str(s, self.sign_positive, &self.format),
@@ -214,3 +231,11 @@ impl std::default::Default for NumberTypeOptionPB {
214231
lazy_static! {
215232
static ref NUM_REGEX: Regex = Regex::new(r"[^\d\.]").unwrap();
216233
}
234+
235+
lazy_static! {
236+
static ref SCIENTIFIC_NOTATION_REGEX: Regex = Regex::new(r"([+-]?\d*\.?\d+)e([+-]?\d+)").unwrap();
237+
}
238+
239+
lazy_static! {
240+
static ref EXTRACT_NUM_REGEX: Regex = Regex::new(r"^(\d+\.\d+)(?:\.\d+)*$").unwrap();
241+
}

0 commit comments

Comments
 (0)