1

problem with retrieving decimal value.

Example:

ufixed8x2 profit = 0;
uint256 amount = 5000000; //msg.value;
 uint256 constant private PERCENT_DIVIDER = 1; 

profit= ufixed8x2(amount.div(24).div(60).div(60).mul(PERCENT_DIVIDER).div(100));

Output is 0 - wrong must be 0.57

Please help

0
1

With uint256 amount = 5000000,

the type of the expression amount.div(24).div(60).div(60).mul(1) is uint256,

and its value is 57.

And when you divide uint256(57) by 100, the result is obviously 0.

I don't know anything about the type ufixed8x2, but assuming that it handles arithmetic operations correctly, you should cast the intermediate result of 57 to this type before dividing it by 100.

In other words, change this:

profit = ufixed8x2(amount.div(24).div(60).div(60).mul(PERCENT_DIVIDER).div(100));

To this:

profit = ufixed8x2(amount.div(24).div(60).div(60).mul(PERCENT_DIVIDER)).div(100);

At least in other strongly-typed languages, this method does the job.

If the div method is not implemented for the ufixed8x2 type, then you can either implement it for this type, or just replace it with a simple / (which you may as well do for all the other occurrences of this method in your code, since there is no benefit in calling it with a constant value - it is a pure waste of gas).

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.