如何在rust中修复Uint128变量的小数点



Rust新手。我正在尝试计算基于rate和rate中的小数点的值扣除的金额。

没有小数点,假设我想从原始中扣除2%的金额(其中2是比率),这就是我如何完成它:

 let original = token_value[0].amount;
 token_value[0].amount = original * Decimal::percent(98);

如果我想在rate中定义小数点,然后进行扣除额,我该怎么做呢?我使用的箱子是cosmwasm_std::{Decimal,Uint128}

如果您想收取原金额的2%作为费用

你可以做

// Here we hardcode the swap fees as 2/100 or 2% of from_amount
fn get_swap_fee(from_amount: Uint128) -> StdResult<Uint128> {
        from_amount
            .checked_mul(Uint128::from(2u128))
            .map_err(StdError::overflow)?
            .checked_div(Uint128::from(100u128))
            .map_err(StdError::divide_by_zero)
    }