{"id":7331,"date":"2024-03-19T09:18:37","date_gmt":"2024-03-19T09:18:37","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7331"},"modified":"2025-01-23T09:24:16","modified_gmt":"2025-01-23T09:24:16","slug":"does-solidity-support-floating-point-numbers","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/does-solidity-support-floating-point-numbers\/","title":{"rendered":"Does Solidity Support Floating Point Numbers? Explained"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/does-solidity-support-floating-point-numbers\/#The_Floating-Point_Fumble_Why_Solidity_Doesnt_Like_Decimals\" title=\"The Floating-Point Fumble: Why Solidity Doesn&#8217;t Like Decimals\">The Floating-Point Fumble: Why Solidity Doesn&#8217;t Like Decimals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/does-solidity-support-floating-point-numbers\/#Working_with_Numbers_in_Solidity_The_Art_of_the_Integer\" title=\"Working with Numbers in Solidity: The Art of the Integer\">Working with Numbers in Solidity: The Art of the Integer<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/does-solidity-support-floating-point-numbers\/#Choosing_the_Right_Tool_for_the_Job_When_Integers_Rule_and_When_Workarounds_Work\" title=\"Choosing the Right Tool for the Job: When Integers Rule and When Workarounds Work\">Choosing the Right Tool for the Job: When Integers Rule and When Workarounds Work<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/does-solidity-support-floating-point-numbers\/#The_Future_of_Floating-Point_in_Solidity_A_Work_in_Progress\" title=\"The Future of Floating-Point in Solidity: A Work in Progress\">The Future of Floating-Point in Solidity: A Work in Progress<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/does-solidity-support-floating-point-numbers\/#In_Conclusion_Embrace_the_Power_of_Integers_But_Be_Aware_of_the_Workarounds\" title=\"In Conclusion: Embrace the Power of Integers, But Be Aware of the Workarounds\">In Conclusion: Embrace the Power of Integers, But Be Aware of the Workarounds<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Solidity, the language that powers smart contracts on the Ethereum blockchain, is a powerful tool for building decentralized applications. But unlike many traditional programming languages,  floating point numbers in Solidity are not supported. This might seem strange at first, considering how common decimals are in the real world. So, what&#8217;s the deal?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-floating-point-fumble-why-solidity-doesn-t-like-decimals\"><span class=\"ez-toc-section\" id=\"The_Floating-Point_Fumble_Why_Solidity_Doesnt_Like_Decimals\"><\/span>The Floating-Point Fumble: Why Solidity Doesn&#8217;t Like Decimals<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Imagine a number line. Integers, like 1, 2, or 3, are whole numbers with neat little marks on that line. Floating-point numbers, like 3.14 (pi) or 1.23, can fall anywhere in between those marks. They use a combination of a base number (like 10) and an exponent (like the power of 10) to represent decimals efficiently.<\/p>\n\n\n\n<p>Computers, however, understand things in terms of 0s and 1s. Representing all the possible in-between values of a floating-point number with perfect accuracy can be tricky. This is where the &#8220;fumble&#8221; comes in. There&#8217;s a trade-off between precision (how many decimal places you can store) and efficiency (how much space it takes up in memory).<\/p>\n\n\n\n<p>Solidity, designed for secure and predictable execution on the blockchain, prioritizes determinism and avoids the potential for errors that floating-point calculations can introduce. The blockchain is a public ledger, and every transaction needs to be verifiable by everyone on the network. Inconsistent results due to floating-point approximations could wreak havoc in this environment.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/one-piece-personality-dapp-solidity\" target=\"_blank\" rel=\"noreferrer noopener\">Build a One Piece Personality dApp With Solidity<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"working-with-numbers-in-solidity-the-art-of-the-integer\"><span class=\"ez-toc-section\" id=\"Working_with_Numbers_in_Solidity_The_Art_of_the_Integer\"><\/span>Working with Numbers in Solidity: The Art of the Integer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, how do Solidity developers handle situations that seem to require decimals? Here are a couple of approaches:<\/p>\n\n\n\n<p><strong>Fixed-Point Math:<\/strong> Imagine zooming in on that number line. Instead of representing decimals directly, Solidity uses integers and keeps track of the decimal places &#8220;behind the scenes.&#8221; For example, to represent 1.23, you could store the integer 123 and consider it as 123\/100 (1.23). This approach ensures predictable calculations but requires careful scaling and tracking of decimal positions. This example calculates a simple interest payment on a loan amount. We&#8217;ll define a fixed-point variable with 18 decimal places (represented by a factor of 10^18).<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:16px\"><code>pragma solidity ^0.8.0;\n\ncontract FixedPointExample {\n\n  \/\/ Define a fixed-point variable with 18 decimals\n  uint256 public constant DECIMALS = 1e18;\n\n  function calculateInterest(uint256 principal, uint256 interestRate, uint256 timeInYears) public pure returns (uint256) {\n    \/\/ Interest rate is assumed to be a percentage (e.g., for 5% interest, rate = 5)\n    uint256 interest = principal * interestRate * timeInYears \/ DECIMALS \/ 100;  \/\/ Adjust for decimals and percentage\n    return interest;\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We define a constant&nbsp;<code>DECIMALS<\/code>&nbsp;to represent the number of decimal places (1e18 = 1 with 18 zeros).<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<code>calculateInterest<\/code>&nbsp;function takes the principal amount, interest rate (as a percentage), and time in years.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>To handle decimals, we multiply the principal by the interest rate and time, but we also divide by&nbsp;<code>DECIMALS<\/code>&nbsp;and 100 to adjust for the fixed-point representation and percentage basis.<\/li><\/ul>\n\n\n\n<p><strong>Libraries for Floating-Point-like Behavior:<\/strong> While Solidity itself doesn&#8217;t have built-in floating-point functionality, there are external libraries that developers can integrate. These libraries offer functions that mimic floating-point behavior using clever integer manipulations. However, it&#8217;s important to remember that these are still workarounds with their own limitations and potential for errors.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:&nbsp;<a href=\"https:\/\/metaschool.so\/courses\/build-hogwarts-sorting-cap-dapp-on-polygon-mumbai\" target=\"_blank\" rel=\"noreferrer noopener\">Build Hogwarts Sorting Hat dApp on Polygon<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"choosing-the-right-tool-for-the-job-when-integers-rule-and-when-workarounds-work\"><span class=\"ez-toc-section\" id=\"Choosing_the_Right_Tool_for_the_Job_When_Integers_Rule_and_When_Workarounds_Work\"><\/span>Choosing the Right Tool for the Job: When Integers Rule and When Workarounds Work<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s a good rule of thumb: if your smart contract deals with whole numbers like token quantities or voting tallies, integers are perfectly suitable. However, if you need calculations involving precise decimals, like financial transactions or scientific computations, things get trickier.<\/p>\n\n\n\n<p>In such cases, carefully consider the trade-offs:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Fixed-point math:<\/strong>&nbsp;Offers good control and predictability, but requires extra coding effort and potential for overflow errors if not handled properly.<\/li><li><strong>External libraries:<\/strong>&nbsp;Can provide a more familiar floating-point-like experience, but introduce additional complexity and potential security vulnerabilities if not chosen and implemented carefully.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-future-of-floating-point-in-solidity-a-work-in-progress\"><span class=\"ez-toc-section\" id=\"The_Future_of_Floating-Point_in_Solidity_A_Work_in_Progress\"><\/span>The Future of Floating-Point in Solidity: A Work in Progress<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The world of blockchain technology is constantly evolving. There&#8217;s an ongoing discussion about introducing fixed-point numbers as a first-class citizen in future versions of Solidity. This would streamline the process of working with decimals while maintaining the core principles of security and determinism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"in-conclusion-embrace-the-power-of-integers-but-be-aware-of-the-workarounds\"><span class=\"ez-toc-section\" id=\"In_Conclusion_Embrace_the_Power_of_Integers_But_Be_Aware_of_the_Workarounds\"><\/span>In Conclusion: Embrace the Power of Integers, But Be Aware of the Workarounds<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The lack of support for floating point numbers in Solidity might seem like a hurdle at first. However, it&#8217;s a deliberate design choice that prioritizes security and predictability for decentralized applications. With a solid understanding of integer arithmetic and the available workarounds, you can write robust smart contracts that handle even complex calculations involving decimals. As the blockchain ecosystem matures, we might see more native support for fixed-point numbers in the future, but for now, integers reign supreme in the world of Solidity.<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and tell us how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013&nbsp;<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013&nbsp;<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":16,"featured_media":7332,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[51,31,46,29],"class_list":["post-7331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-ethereum-blockchain","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7331"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7331\/revisions"}],"predecessor-version":[{"id":12015,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7331\/revisions\/12015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7332"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}