Ether
Ether的单位关键字有wei, finney, szabo, ether,换算格式如下:
- 1 ether = 1 * 10^18 wei
- 1 ether = 1 * 10^6 szabo
- 1 ether = 1* 10^3 finney
示例:
pragma solidity 0.4.20;
/**
* 对 比特币 Ether 的几个单位进行测试
*/
contract testEther {
// 定义全局变量
uint public balance;
function testEther() public{
balance = 1 ether; //1000000000000000000
}
function fFinney() public{
balance = 1 finney; //1000000000000000
}
function fSzabo() public{
balance = 1 szabo; //1000000000000
}
function fWei() public{
balance = 1 wei; //1
}
}
Time
Time的单位关键字有seconds, minutes, hours, days, weeks, years,换算格式如下:
- 1 == 1 seconds
- 1 minutes == 60 seconds
- 1 hours == 60 minutes
- 1 days == 24 hours
- 1 weeks == 7 days
- 1 years == 365 days
如果你需要进行使用这些单位进行日期计算,需要特别小心,因为不是每年都是365天,且并不是每天都有24小时,因为还有闰秒。由于无法预测闰秒,必须由外部的oracle来更新从而得到一个精确的日历库(内部实现一个日期库也是消耗gas的)。
示例:
pragma solidity 0.4.20;
/**
* 对 Time 单位进行测试
*/
contract testTime {
// 定义全局变量
uint time;
function testTime() public{
time = 100000000;
}
function fSeconds() public view returns(uint){
return time + 1 seconds; //100000001
}
function fMinutes() public view returns(uint){
return time + 1 minutes; //100000060
}
function fHours() public view returns(uint){
return time + 1 hours; //100003600
}
function fWeeks() public view returns(uint){
return time + 1 weeks; //100604800
}
function fYears() public view returns(uint){
return time + 1 years; //131536000
}
}
在Browser-solidity中调试: