5

This question already has an answer here:

I want to store the purchase price of a digital asset within the contract and then allow a user to purchase it. I have the following struct with some demo hard coded values but using a uint for the purchasePrice field isn't working

Asset memory _asset = Land({
                geoPoint1: 'ldsfsdfsdf',
                geoPoint2: 'ldsfsdfsdf',
                geoPoint3: 'ldsfsdfsdf',
                geoPoint4: 'ldsfsdfsdf',
                creationTime: uint64(now),
                itemdemo: 100000,
                purchasePrice: 1.20
            });

marked as duplicate by eth Jan 5 '18 at 9:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7

Deep down, everything is uint.

In practice, what this means is you should establish the minimum unit and maximum divisibility your app supports. For example, a "dollar" (generally) has two decimals (penny) and no further division is (generally) possible.

Suppose you want up to 4 decimal places. Use integers raised by 10**4. It is a best practice to externalize this concern from the contract. That is, leave it to clients to convert contract style numbers to human-readable notation.

purchasePrice: 12000 // using 4 decimal places consistently

The ERC20 token standard and ethereum itself both use fixed decimal places and integer math internally, relying on clients to convert to human-readable notation. ERC20 leaves the decision about the number of decimals to the developer. That global parameter signals wallets and exchanges so they can know how to present the integer values.

Hope it helps.

  • 1
    Thanks, I basically want to set the price in Ether so for example it may be 0.1 Eth. – ORStudios Jan 4 '18 at 20:31
  • 2
    That's a 1 with 16 0's after it. The base unit is called a "wei". – Rob Hitchens - B9lab Jan 5 '18 at 0:38
  • 1
    Ether has 18 decimals, not 16. – Mikhail Vladimirov Apr 22 at 3:31
  • 1
    Yes, it does. When OP posted 0.1, I miscounted 0.1 as 2 digits... 17 more 0 needed in that case. – Rob Hitchens - B9lab Apr 22 at 6:35

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