All numbers in Tact are integers. Floating point types are not used in smart contracts because they're unpredictable.
Arithmetics with dollars, for example, requires 2 decimal places. How can we represent the number 1.25
if we can only work with integers? The answer is to work with cents. So 1.25
becomes 125
. We just remember that the two lowest digits are coming after the decimal point.
In the same way, working with TON coins has 9 decimal places instead of 2. So the amount 1.25 TON is actually the number 1250000000
- we call these nano-tons instead of cents.
This example calculates the earned interest over a deposit of 500 TON coins. The yearly interest rate in the example is 3.25%.
Since we can't hold the number 3.25
we will use thousandth of a percent as unit (percent-mille). So 3.25% becomes 3250
(3.25 * 1000).
On withdraw, to calculate earned interest, we multiply the amount by the fraction of a year that passed (duration in seconds divided by total seconds in a year) and then by the interest rate divided by 100,000 (100% in percent-mille, meaning 100 * 1000).
import "@stdlib/deploy"; struct TokenInfo { ticker: String; decimals: Int as uint8; } // this is a global static function that can be called from anywhere fun average(a: Int, b: Int): Int { return (a + b) / 2; } contract Functions with Deployable { deployer: Address; init() { self.deployer = sender(); } // this contract method can be called from within this contract and access its variables fun onlyDeployer() { require(sender() == self.deployer, "Only the deployer is permitted here"); } receive("priviliged") { self.onlyDeployer(); } // this contract method returns multiple return values using a struct fun getInfo(index: Int): TokenInfo { if (index == 1) { return TokenInfo{ticker: "TON", decimals: 9}; } if (index == 2) { return TokenInfo{ticker: "ETH", decimals: 18}; } return TokenInfo{ticker: "unknown", decimals: 0}; } receive("best L1") { let best: TokenInfo = self.getInfo(1); self.reply(best.ticker.asComment()); } get fun result(): Int { return average(1, 10); } }