Skip to content
Snippets Groups Projects

Fixing precision issue when converting the u64 cents amount into f32.

Merged Nicolas80 requested to merge Nicolas80/gcli-v2s:fixing_balance_value_formatting into master

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

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Author Maintainer

    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...

  • Ça me semble bien avec le bête modulo, on n'a pas besoin de flottants pour ça. Peut-être une bibliothèque de décimaux à virgule fixe, mais si ce n'est que pour l'affichage c'est overkill.

  • Hugo Trentesaux approved this merge request

    approved this merge request

  • Nicolas80 mentioned in commit 8f67e583

    mentioned in commit 8f67e583

Please register or sign in to reply
Loading