Skip to content

Commit 3099256

Browse files
authored
Fix toString for FixedPoint instances with 0 decimals (#1472)
* Fix `toString` for instances with 0 decimals * Rebuild * Add changeset
1 parent 71bf6a9 commit 3099256

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

.changeset/real-files-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@delvtech/fixed-point-wasm": patch
3+
---
4+
5+
Fix `toString()` for instances with `0` decimals.

crates/fixed-point-wasm/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,17 @@ impl WasmFixedPoint {
344344
/// Get the decimal string representation of this fixed-point number.
345345
#[wasm_bindgen(skip_jsdoc, js_name = toString)]
346346
pub fn to_string(&self) -> String {
347-
let decimals_delta = INNER_DECIMALS - self.decimals;
348347
let str = self.inner.to_string();
348+
349+
// If there are no decimals, return the integer part only.
350+
if self.decimals == 0 {
351+
return str.split('.').next().unwrap_or_default().to_string();
352+
}
353+
354+
let decimals_delta = INNER_DECIMALS - self.decimals;
355+
if decimals_delta == 0 {
356+
return str;
357+
}
349358
str[..str.len() - decimals_delta as usize].to_string()
350359
}
351360

packages/fixed-point-wasm/fixed_point_wasm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)