智能合约语言 Solidity 以太单位及时间单位

想知道更多区块链技术问题,请百度【链客区块链技术技术问答社区】

Solidity是以太坊智能合约编程语言,当然你在阅读这篇文章之前,你应该对以太坊、智能合约有初步或者深入的了解,当然你还是不了解的话,建议你先去看以太坊是什么。

时间单位
时间单位: seconds, minutes, hours, days, weeks, years均可做为后缀,并进行相互转换,规则如下:
1 == 1 seconds (默认是seconds为单位)
1 minutes == 60 seconds
1 hours == 60 minutes
1 days == 24 hours
1 weeks = 7 days
1 years = 365 days
使用这些单位进行日期计算需要特别小心,因为不是每年都是365天,闰年是366天,且并不是每天都有24小时,因为还有闰秒。由于无法预测闰秒,必须由外部的预言来更新从而得到一个精确的日历库。
这些后缀不能用于变量。如果想对输入的变量说明其不同的单位,可以使用下面的方式:
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)
{
// …
}
}
}

货币单位
一个字面量后面跟随一个后缀wei、finney、szabo或者ether,这些后缀就是货币单位,不同的单位可以转换。不含任何后缀的默认单位是wei。
不同的以太币单位转换关系如下:
1 ether == 10^18 wei
1 ether == 10^6 szabo
1 ether == 10^3 finney
1 ether == 1000 finney
我们可以使用一下代码验证一个转换关系:
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;
}
}

猜你喜欢

转载自blog.csdn.net/weixin_44172023/article/details/85989642