Fixing precision issue when converting the u64 cents amount into f32.
Now composing the displayed value with integer part and fractional part separately (keeping left padding of 0 for the decimal part for the amount of decimals required)
Potential issue with this version is that we don't use the locale to decide on the decimal separator (hardcoded to "." here)
Tested locally and resolve the issue with big amounts.
Example before:
gcli account balance
5Funkng8dL397H9ZEJ9sXDjFhStCqRYUj1a62ngcrEMy7ivn has 11233705000 ĞD
After:
gcli account balance
5Funkng8dL397H9ZEJ9sXDjFhStCqRYUj1a62ngcrEMy7ivn has 11233705361.58 ĞD
Merge request reports
Activity
Filter activity
Alternative possible en utilisant la dépendance
bigdecimal = "0.4.6"
pub fn format_balance(&self, amount: Balance) -> String { let base: u64 = 10; let amount_in_cents = BigDecimal::from(amount) .with_scale(self.token_decimals as i64); let divider = BigDecimal::from(base.pow(self.token_decimals)).with_scale(self.token_decimals as i64); let amount_bd = amount_in_cents.div(divider); format!( "{} {}", amount_bd, self.token_symbol ) }
Mais je ne sais pas ce qui est préférable...
mentioned in commit 8f67e583
Please register or sign in to reply