package main
import (
"fmt"
"os"
"strconv"
)
type Feet float64
type Meter float64
func main() {
var transType string = os.Args[2]
var transferObject string = os.Args[1]
parseObj, _ := strconv.ParseFloat(transferObject, 64)
if transType == "feet" {
// transfer to meter
fmt.Println(toFeet(Meter(parseObj)))
} else {
fmt.Println(toMeter(Feet(parseObj)))
}
}
func toMeter(feet Feet) Meter {
return Meter(feet * 0.3048)
}
func toFeet(meter Meter) Feet {
return Feet(meter * 1 / 0.3048)
}
// 保存后 go run test.go 1 feet 输出1米=多少英尺
// 字节的单位转换 保留两位小数
func formatFileSize(fileSize int64) (size string) {
if fileSize 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}