Anu
Anu

Reputation: 33

How to handle decimal numbers in 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.

Upvotes: 3

Views: 7266

Answers (1)

user94559
user94559

Reputation: 60153

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

Upvotes: 4

Related Questions