Smart Contract Language Solidity Tutorial Series 7-etheric units and time units

Source: Internet
Author: User

This is Solidity Tutorial series article 7th introduces the etheric units and time units, series with you to thoroughly understand the solidity language.
For a complete list of articles in the solidity series, see Category-solidity.

Write in front

Solidity is the Ethereum Smart Contract programming language, you should know about Ethereum and smart contracts before reading this article.
If you don't understand, it's recommended that you look at Ethereum first.

Monetary unit (ether Units)

A numeric constant (literal) follows a suffix wei, Finney,Szabo , or ether, which is the currency unit. Different units can be converted. The default unit that does not contain any suffixes is wei.
The different etheric unit conversion relationships are as follows:

    • 1 ether = = 10^3 Finney = = Finney
    • 1 ether = = 10^6 Szabo
    • 1 ether = = 10^18 Wei

Episode: The Ethereum unit is actually the name of the cipher, and is the founder of Ethereum to commemorate their contribution in the field of digital money. They were:
Wei:wei Dai either David Cipher, published in B-money
Finney:hal Finney, work proof mechanism (POW) proposed
Szabo:nick Szabo, the creator of the smart contract for the Saab

We can use the code to verify a transformation relationship:

pragma solidity ^0.4.16;contract testUnit {    function tf() public pure returns (bool) {      if (1 ether == 1000 finney){          return true;      }      return false;    }    function ts() public pure returns (bool) {      if (1 ether == 1000000 szabo){          return true;      }      return false;    }    function tgw() public pure returns (bool) {      if (1 ether == 1000000000000000000 wei){          return true;      }      return false;    }}
Time Unit (Units)

Time units: seconds, minutes, hours, days, weeks, years can be used as suffixes, and to convert each other, the rules are as follows:

    • 1 = = 1 seconds (default is seconds)
    • 1 minutes = = seconds
    • 1 hours = = Minutes
    • 1 days = = Hours
    • 1 weeks = 7 days
    • 1 years = 365 days

Using these units for date calculations requires special care, because not every year is 365 days, and not every day there are 24 hours, because there are leap seconds. Because the leap seconds cannot be predicted, it must be updated by the External prophecy (Oracle) to get an accurate calendar library.

These suffixes cannot be used for variables. If you want to explain the different units of the input variables, you can use the following method:

pragma solidity ^0.4.16;contract testTUnit {    function currTimeInSeconds() public pure returns (uint256){        return now;    }    function f(uint start, uint daysAfter) public {        if (now >= start + daysAfter * 1 days) {        // ...        }    }}
Reference documents
    • Units
    • In-depth blockchain-the system learns blockchain to create the best blockchain technology blog.

Smart Contract Language Solidity Tutorial Series 7-etheric units and time units

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.