0

If I wanted to do some basic arithmetic operations on some weight it would be possible to do so using saturating_* methods.

But what if I wanted to multiply it with a fraction(e.g. Perbill)? How could I convert the weight into u64 so that I can do the above mentioned operation, and how could I convert it back to the Weight type?

1 Answer 1

2

How to convert Weight to u64 and the other way around

You cannot convert Weigth to a single u64, since it is (currently) two dimensional. You can split and construct it from its components like this:

let weight = Weight::from_parts(10, 20);
let ref_time = weight.ref_time();
let proof_size = weight.proof_size();
let weight = Weight::from_parts(ref_time, proof_size);

But what if I wanted to multiply it with a fraction(e.g. Perbill)?

Well... just do it?

#[test]
fn per_thing_mul_works() {
    let weight = Weight::from_parts(80, 60);
    let half = sp_arithmetic::Perbill::from_percent(50);

    assert_eq!(weight * half, Weight::from_parts(40, 30));
    assert_eq!(half * weight, Weight::from_parts(40, 30));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.