Skip to content

Commit 2d43c56

Browse files
committed
fix(wasm-shim): correctly round decimals
1 parent 8198bf3 commit 2d43c56

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

crates/wasm-shim/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasm-shim"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2021"
55

66
[lib]
@@ -12,6 +12,7 @@ muropeptide = { path = "../muropeptide" }
1212
polychem = { path = "../polychem" }
1313
smithereens = { path = "../smithereens" }
1414
console_error_panic_hook = "0.1.7"
15+
rust_decimal = "1.36.0"
1516

1617
[lints]
1718
workspace = true

crates/wasm-shim/src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use muropeptide::{Muropeptide, POLYMERIZER};
22
use polychem::{ChargedParticle, Massive};
3+
use rust_decimal::Decimal;
34
use smithereens::Dissociable;
45
use std::fmt::Write;
56
use wasm_bindgen::prelude::*;
@@ -35,7 +36,7 @@ impl Peptidoglycan {
3536
#[must_use]
3637
pub fn monoisotopic_mass(&self) -> String {
3738
let mass = self.0.monoisotopic_mass();
38-
format!("{mass:.6}")
39+
decimal_round_workaround(mass, 6)
3940
}
4041

4142
#[must_use]
@@ -53,8 +54,15 @@ impl Peptidoglycan {
5354
let mut csv = String::new();
5455
writeln!(&mut csv, "Structure,Ion M/Z").unwrap();
5556
for (structure, mz) in fragments {
56-
writeln!(&mut csv, r#""{structure}",{mz:.6}"#).unwrap();
57+
let mz = decimal_round_workaround(mz, 6);
58+
writeln!(&mut csv, r#""{structure}",{mz}"#).unwrap();
5759
}
5860
csv
5961
}
6062
}
63+
64+
// FIXME: Really this should be fixed in `rust_decimal`...
65+
fn decimal_round_workaround(value: impl Into<Decimal>, decimal_points: u32) -> String {
66+
let value = value.into().round_dp(decimal_points);
67+
format!("{value}")
68+
}

0 commit comments

Comments
 (0)