Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle decimal numbers in solidity?

Tags:

solidity

How to handle decimal numbers in solidity? If you want to find the percentage of some amount and do some calculation on that number, how to do that?

Suppose I perform : 15 % of 45 and need to divide that value with 7 how to get the answer.

Please help. I have done research, but getting answer like it is not possible to do that calculation. Please help.

like image 737
Anu Avatar asked Dec 08 '25 06:12

Anu


1 Answers

You have a few options. To just multiply by a percentage (but truncate to an integer result), 45 * 15 / 100 = 6 works well. (45 * 15%)

If you want to keep some more digits around, you can just scale everything up by, e.g., some exponent of 10. 4500 * 15 / 100 = 675 (i.e. 6.75 * 100).

like image 113
user94559 Avatar answered Dec 12 '25 15:12

user94559



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!