{"id":4595,"date":"2023-01-12T05:56:56","date_gmt":"2023-01-12T05:56:56","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=4595"},"modified":"2023-08-31T10:21:18","modified_gmt":"2023-08-31T10:21:18","slug":"convert-wei-ethereum-php","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/convert-wei-ethereum-php\/","title":{"rendered":"How can I convert Wei to Ethereum with PHP?"},"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\/convert-wei-ethereum-php\/#Whats_bcmath\" title=\"What&#8217;s bcmath?\">What&#8217;s bcmath?<\/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\/convert-wei-ethereum-php\/#Some_of_the_commonly_used_functions_in_bcmath\" title=\"Some of the commonly used functions in bcmath\">Some of the commonly used functions in bcmath<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/convert-wei-ethereum-php\/#General_steps_to_convert_Wei_to_Ethereum_with_PHP_bcmath_extension\" title=\"General steps to convert Wei to Ethereum with PHP bcmath extension\">General steps to convert Wei to Ethereum with PHP bcmath extension<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/convert-wei-ethereum-php\/#1_Make_sure_the_bcmath_extension_is_enabled_on_your_PHP_installation\" title=\"1. Make sure the bcmath extension is enabled on your PHP installation\">1. Make sure the bcmath extension is enabled on your PHP installation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/convert-wei-ethereum-php\/#2_Use_the_bcdiv_function_to_divide_the_Wei_value_by_1018_since_there_are_1018_Wei_in_1_Ether\" title=\"2. Use the bcdiv() function to divide the Wei value by 10^18, since there are 10^18 Wei in 1 Ether. \">2. Use the bcdiv() function to divide the Wei value by 10^18, since there are 10^18 Wei in 1 Ether. <\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/convert-wei-ethereum-php\/#3_You_can_also_use_the_bcmul_function_to_multiply_the_ether_value_to_convert_back_to_wei\" title=\"3. You can also use the bcmul() function to multiply the ether value to convert back to wei\">3. You can also use the bcmul() function to multiply the ether value to convert back to wei<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>In PHP, you can convert Wei to <a href=\"https:\/\/metaschool.so\/blockchains\/ethereum?ref=Articles&amp;utm_source=Blog_Organic\" target=\"_blank\" rel=\"noreferrer noopener\">Ethereum<\/a> using the bcmath extension, which provides support for arbitrary precision math. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-s-bcmath\"><span class=\"ez-toc-section\" id=\"Whats_bcmath\"><\/span>What&#8217;s bcmath?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The bcmath extension is a PHP extension that provides a set of functions that allow developers to perform mathematical operations with arbitrary precision, which is useful when working with large numbers or decimal numbers that require a high degree of accuracy.<\/p>\n\n\n\n<p>The bcmath functions use strings to represent numbers, allowing them to handle numbers with an arbitrary number of digits. This makes them useful for tasks such as financial calculations, cryptography, and other applications that require precise calculations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"some-of-the-commonly-used-functions-in-bcmath\"><span class=\"ez-toc-section\" id=\"Some_of_the_commonly_used_functions_in_bcmath\"><\/span>Some of the commonly used functions in bcmath<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>bcadd() :<\/strong> adds two arbitrary precision numbers<\/li><li><strong>bcsub() :<\/strong> subtracts one arbitrary precision number from another<\/li><li><strong>bcmul() :<\/strong> multiplies two arbitrary precision numbers<\/li><li><strong>bcdiv() :<\/strong> divides one arbitrary precision number by another<\/li><li><strong>bcmod() :<\/strong> computes the modulus of an arbitrary precision number<\/li><li><strong>bcpow() :<\/strong> raises an arbitrary precision number to a power<\/li><li><strong>bcsqrt() :<\/strong> computes the square root of an arbitrary precision number<\/li><li><strong>bcscale() :<\/strong> sets the default scale used by all bcmath functions<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"general-steps-to-convert-wei-to-ethereum-with-php-bcmath-extension\"><span class=\"ez-toc-section\" id=\"General_steps_to_convert_Wei_to_Ethereum_with_PHP_bcmath_extension\"><\/span>General steps to convert Wei to Ethereum with PHP bcmath extension<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-make-sure-the-bcmath-extension-is-enabled-on-your-php-installation\"><span class=\"ez-toc-section\" id=\"1_Make_sure_the_bcmath_extension_is_enabled_on_your_PHP_installation\"><\/span>1. Make sure the bcmath extension is enabled on your PHP installation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.php.net\/manual\/en\/book.bc.php\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">bcmath extension<\/a> is not enabled by default in PHP, so you will need to check the output of <code>phpinfo()<\/code> or by adding <code>extension=bcmath.so<\/code> in your php.ini file to check if the extension is enabled, and if not, you&#8217;ll need to enable it before using these functions in your code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-use-the-bcdiv-function-to-divide-the-wei-value-by-10-18-since-there-are-10-18-wei-in-1-ether\"><span class=\"ez-toc-section\" id=\"2_Use_the_bcdiv_function_to_divide_the_Wei_value_by_1018_since_there_are_1018_Wei_in_1_Ether\"><\/span>2. Use the bcdiv() function to divide the Wei value by 10^18, since there are 10^18 Wei in 1 Ether. <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>$wei = \"1000000000000000000\";\n$ether = bcdiv($wei, \"1000000000000000000\", 18);<\/code><\/pre>\n\n\n\n<p>Note that the third parameter &#8220;18&#8221; is the precision, the amount of decimal places the result will have.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-you-can-also-use-the-bcmul-function-to-multiply-the-ether-value-to-convert-back-to-wei\"><span class=\"ez-toc-section\" id=\"3_You_can_also_use_the_bcmul_function_to_multiply_the_ether_value_to_convert_back_to_wei\"><\/span>3. You can also use the bcmul() function to multiply the ether value to convert back to wei<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>$wei = bcmul($ether, \"1000000000000000000\",0);<\/code><\/pre>\n\n\n\n<p>Here the last parameter &#8220;0&#8221; is to round off the resultant value to zero decimal places.<\/p>\n\n\n\n<p>A practical example in your code would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>&lt;?php\n    $wei = \"1000000000000000000\";\n    $ether = bcdiv($wei, \"1000000000000000000\", 18);\n    echo $ether;\n    $wei = bcmul($ether, \"1000000000000000000\",0);\n    echo $wei;\n?&gt;<\/code><\/pre>\n\n\n\n<p>It would output &#8220;1&#8221; and &#8220;1000000000000000000&#8221; respectively<\/p>\n\n\n\n<p>Please note that as Ethereum uses floating point numbers and the bcmath uses decimal numbers, in some cases it might not be precise for very high numbers or for very low precision.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":4597,"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":[],"class_list":["post-4595","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/4595","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=4595"}],"version-history":[{"count":5,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/4595\/revisions"}],"predecessor-version":[{"id":6810,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/4595\/revisions\/6810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/4597"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=4595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=4595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=4595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}