Skip to content

Commit

Permalink
fix to_string() for long decimals
Browse files Browse the repository at this point in the history
fixes #13
  • Loading branch information
YaroShkvorets committed Mar 20, 2024
1 parent 9a696fd commit 9d61b7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "antelope"
version = "0.1.5"
version = "0.1.6"
authors = ["Denis <[email protected]>", "Yaro <[email protected]>"]
description = "Antelope Standard Library"
homepage = "https://github.com/pinax-network/antelope.rs"
Expand Down
32 changes: 18 additions & 14 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ impl std::fmt::Display for Asset {
* @return String in the form of "1.2345 SYM" format, where SYM symbol has precision equal to 4
*/
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let precision = self.symbol.precision();
let decimals = 10_i64.pow(precision.into());
let int_part = self.amount / decimals;
let dec_part = (self.amount % decimals).abs();
let whole = self.amount / 10_i64.pow(self.symbol.precision().min(18) as u32);

if precision == 0 {
write!(f, "{} {}", int_part, self.symbol.code())
let decimal: String = (0..self.symbol.precision() as usize)
.rev()
.map(|i| (self.amount.abs() / 10_i64.pow(i.min(18) as u32)) % 10)
.map(|digit| (b'0' + (digit as u8)) as char)
.collect();

if decimal.is_empty() {
write!(f, "{} {}", whole, self.symbol.code())
} else {
write!(f, "{}.{:0pad$} {}", int_part, dec_part, self.symbol.code(), pad = precision.into())
write!(f, "{}.{} {}", whole, decimal, self.symbol.code())
}
}
}
Expand Down Expand Up @@ -152,12 +155,7 @@ impl std::cmp::PartialEq for Asset {

impl std::cmp::PartialOrd for Asset {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
check(
self.symbol == other.symbol,
"comparison of assets with different symbols is not allowed",
);

self.amount.partial_cmp(&other.amount)
Some(self.cmp(other))
}
}

Expand Down Expand Up @@ -771,11 +769,11 @@ mod tests {

#[test]
fn test_to_string() {
assert_eq!(Asset::from_amount(-1000001, Symbol::from("4,SYM")).to_string(), "-100.0001 SYM");
assert_eq!(Asset::from_amount(10000, Symbol::from("4,SYM")).to_string(), "1.0000 SYM");
assert_eq!(Asset::from_amount(0, Symbol::from("4,SYM")).to_string(), "0.0000 SYM");
assert_eq!(Asset::from_amount(12345, Symbol::from("2,SYM")).to_string(), "123.45 SYM");
assert_eq!(Asset::from_amount(100, Symbol::from("0,SYM")).to_string(), "100 SYM");
assert_eq!(Asset::from_amount(-1000001, Symbol::from("4,SYM")).to_string(), "-100.0001 SYM");
assert_eq!(Asset::from_amount(0, Symbol::from("0,SYM")).to_string(), "0 SYM");
assert_eq!(Asset::from_amount(-100, Symbol::from("0,SYM")).to_string(), "-100 SYM");
assert_eq!(
Expand All @@ -801,10 +799,16 @@ mod tests {
assert_eq!(Asset::from_amount(-1000001, Symbol::from("4,SYM")), Asset::from("-100.0001 SYM"));
assert_eq!(Asset::from_amount(0, Symbol::from("0,SYM")), Asset::from("0 SYM"));
assert_eq!(Asset::from_amount(0, Symbol::from("4,SYM")), Asset::from("0.0000 SYM"));
assert_eq!(Asset::from_amount(1, Symbol::from("4,SYM")), Asset::from("0.0001 SYM"));
assert_eq!(
Asset::from_amount(-1000000000000000000, Symbol::from("18,SYMBOLL")),
Asset::from("-1.000000000000000000 SYMBOLL")
);
// see: https://github.com/pinax-network/antelope.rs/issues/13
assert_eq!(
Asset::from_amount(10000000000001, Symbol::from("69,JIAYOUY")),
Asset::from("0.000000000000000000000000000000000000000000000000000000010000000000001 JIAYOUY")
);
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/extended_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ impl std::cmp::PartialEq for ExtendedAsset {

impl std::cmp::PartialOrd for ExtendedAsset {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
check(self.contract == other.contract, "type mismatch");
self.quantity.partial_cmp(&other.quantity)
Some(self.cmp(other))
}
}

Expand Down

0 comments on commit 9d61b7d

Please sign in to comment.