Skip to content

Commit a1218cb

Browse files
committed
Add locale support for numbers
1 parent 0fe6785 commit a1218cb

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ gloo-utils = "0.2"
3131
html5ever = "0.25"
3232
infer = "0.16.0"
3333
instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] }
34+
isin = "0.1.18"
3435
js-sys = "0.3"
3536
log = "0.4"
3637
nom = "6.0"
38+
num-format = "0.4.4"
3739
once_cell = "1.5"
3840
pdf-extract = "0.7"
3941
rust_decimal = "1.7"
@@ -45,7 +47,6 @@ wasm-bindgen = "0.2"
4547
wasm-bindgen-futures = "0.4"
4648
wasm-logger = "0.2"
4749
zip = { version = "0.5", default-features = false, features = ["deflate"]}
48-
isin = "0.1.18"
4950

5051
[dev-dependencies]
5152
ctor = "0.1"

src/data.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use chrono::NaiveDate;
2+
use num_format::Locale;
23
use rust_decimal::Decimal;
34
use serde::{Deserialize, Serialize};
45
use std::{convert::From, sync::Arc};
@@ -7,7 +8,9 @@ pub type AccountNotes = Vec<AccountNote>;
78
pub type BalanceNotes = Vec<BalanceNote>;
89
pub type Aeat720Records = Vec<Aeat720Record>;
910

11+
pub const DEFAULT_YEAR: usize = 2024;
1012
pub const SPAIN_COUNTRY_CODE: &str = "ES";
13+
pub const DEFAULT_LOCALE: &Locale = &Locale::es;
1114

1215
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
1316
pub enum BrokerOperation {
@@ -142,8 +145,6 @@ pub struct Aeat720Information {
142145
pub personal_info: PersonalInformation,
143146
}
144147

145-
pub const DEFAULT_YEAR: usize = 2024;
146-
147148
impl Aeat720Information {
148149
pub fn full_name(&self) -> String {
149150
self.personal_info.surname.clone() + " " + &self.personal_info.name

src/table.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use web_sys::{HtmlElement, HtmlInputElement};
1111

1212
use crate::{
1313
css::{TABLE_CAPTION, TABLE_ROW, TABLE_STYLE},
14-
data::{Aeat720Record, BrokerInformation},
15-
utils::{icons::render_svg_trash_icon, usize_to_date},
14+
data::{Aeat720Record, BrokerInformation, DEFAULT_LOCALE},
15+
utils::{decimal::decimal_to_str_locale, icons::render_svg_trash_icon, usize_to_date},
1616
};
1717

1818
const ISIN_NOT_VALID_ERR_MSG: &str = "ISIN no válido";
@@ -250,7 +250,7 @@ impl Table {
250250
.attr("type", "text")
251251
.attr("size", "15")
252252
.attr("maxlength", "15")
253-
.attr("value", &r.record.value_in_euro.to_string())
253+
.attr("value", &decimal_to_str_locale(&r.record.value_in_euro, DEFAULT_LOCALE))
254254
}))
255255
}))
256256
})
@@ -264,7 +264,7 @@ impl Table {
264264
.attr("type", "text")
265265
.attr("size", "15")
266266
.attr("maxlength", "15")
267-
.attr("value", &r.record.quantity.to_string())
267+
.attr("value", &decimal_to_str_locale(&r.record.quantity, DEFAULT_LOCALE))
268268
}))
269269
}))
270270
})

src/utils/decimal.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
use num_format::Locale;
2+
use rust_decimal::Decimal;
3+
14
pub fn transform_i18n_es_str(input: &str) -> String {
25
str::replace(&str::replace(input, ".", ""), ",", ".")
36
}
47

58
pub fn normalize_str(input: &str) -> String {
69
str::replace(input, ",", "")
710
}
11+
12+
pub fn decimal_to_str_locale(number: &Decimal, locale: &Locale) -> String {
13+
let mut result = number.to_string();
14+
if let Some(idx) = result.rfind('.') {
15+
result.replace_range(idx..idx + 1, locale.decimal());
16+
}
17+
18+
result
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn test_decimal_to_str_locale() {
27+
let x = Decimal::new(2314, 2);
28+
assert_eq!("23,14", decimal_to_str_locale(&x, &Locale::es));
29+
}
30+
}

0 commit comments

Comments
 (0)