Skip to content
Cosmopediaby Unity Nodes
DiscussionsCosmos-SDKHow does cosmos sdk handle floating point coin amountsForum ↗

How does cosmos sdk handle floating point coin amounts

Cosmos-SDK2 posts2,519 viewsLast activity Jan 2021
EM
emperorjmOP
Dec 2020

I want to know if the implementation is similar to Ethereum where an unsigned integer is used with a factor to decide on the maximum precision? If this is the design how is the factor changed or will the developer need to set this themself?

I see the sdk.Uint and sdk.Dec implementations and need to know if these are the standard implementations to be used where the sdk.Uint would be used to store the amounts and sdk.Dec would be used to manipulate the amounts if needed in calculations?

MI
MiguelSimplyVC
Jan 2021

sdk.Dec can be used both to store an amount and to manipulate the amounts in calculations. It stores a big.Int (note: not unsigned) and has a fixed 18 decimal points of precision, and I do not think that this can be changed unless you edit the decimal.go file. There is a NewDecWithPrec function, which allows you to set a custom precision, but regardless of precision picked the function will just add zeros to the number’s tail to set it to a precision of 18.

So if I create a new decimal using NewDecWithPrec(1111, 2) (i.e. 1111 with precision 2, for which I expect 11.11), then the resultant decimal will be 11.110000000000000000 (16 zeros appended after the original number, such that resultant precision is 18).

Specifically around coins, there’s the DecCoin (and DecCoins) type which is similar to the normal Coin type but has a Dec amount instead of an Int amount.

← Back to Discussions