I am new in solidity language. Have seen their documentation. Is there any floating point
data type ?
There is no floating point in Solidity. You should keep numbers in whole number format.
You can place decimal place in your front-end code. Take a look at how ERC20 contract was designed.
Good read: https://medium.com/@jgm.orinoco/understanding-erc-20-token-contracts-a809a7310aa5
There is no native support for floating-point numbers in the core language, but they are available via libraries, such as ABDKMathQuad.
You can have floating point constants in Solidity, but only with a unit designation gwei
(which multiplies by 10^9
, or ether
(which multiplies by 10^18
). For example, if you write 0.1 ether
in your Solidity code, it is turned into 100000000000000000
(= 10^17 wei
). However, after multiplication by the fixed point multiplier in the unit suffix, there can't be any fractional decimals left. For example you can't specify 1.00000000000000000000000001 ether
, because that would leave you with a fractional number of wei
.
More generally, what you are probably asking about is how to do fixed point mathematical calculations in Solidity, since floating point is not supported. There are many tutorials online explaining how to calculate fractional values using fixed point (just search for "fixed point arithmetic tutorial", for a wide range of languages (the principles apply directly to Solidity too). You can generally implement fixed point with very little code (addition and subtraction work without any special care, as long as both operands have already been multiplied by the same fixed point multiplier, whereas for multiplying two fixed point numbers, you just divide by the fixed point multiplier after multiplying two fixed point numbers that have already each been multiplied by the same fixed point multiplier). Or you can use one of the math libraries linked in other answers.
© 2022 - 2024 — McMap. All rights reserved.