Ether
Ether的單位關(guān)鍵字有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的單位關(guān)鍵字有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小時,因為還有閏秒。由于無法預(yù)測閏秒,必須由外部的oracle來更新從而得到一個精確的日歷庫(內(nèi)部實現(xiàn)一個日期庫也是消耗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中調(diào)試:

Time