| | | @@ 変更前 2行目〜 / 変更後 2行目〜 @@ |
| 2 | 2 | | |
| 3 | 3 | | import ( |
| 4 | 4 | | "fmt" |
| 5 | | − | "math" |
| 6 | | − | "strconv" |
| 5 | + | "math/big" |
| 7 | 6 | | ) |
| 8 | 7 | | |
| 9 | | − | var tiers = []float64{3, 5, 7, 10, 20, 30, 50, 100} |
| 8 | + | var tiers = []int64{3, 5, 7, 10, 20, 30, 50, 100} |
| 10 | 9 | | |
| 11 | | − | // v1: よくある書き方(float64 で計算し、math.Round と strconv.FormatFloat で表示)。 |
| 12 | | − | func fmtData(gb float64) string { |
| 13 | | − | if gb >= 10 { |
| 14 | | − | return strconv.FormatFloat(gb, 'f', 0, 64) + " GB" |
| 15 | | − | } |
| 16 | | − | if gb >= 1 { |
| 17 | | − | return strconv.FormatFloat(gb, 'f', 1, 64) + " GB" |
| 18 | | − | } |
| 19 | | − | mb := gb * 1024 |
| 20 | | − | if mb >= 100 { |
| 21 | | − | return fmt.Sprintf("%d MB", int64(math.Round(mb))) |
| 22 | | − | } |
| 23 | | − | return strconv.FormatFloat(mb, 'f', 1, 64) + " MB" |
| 10 | + | // v2: math/big.Rat で正確に計算し、四捨五入(0.5 ちょうどは繰り上げ)も自前で行う。 |
| 11 | + | // float64 だと 0.08 × 3.5 × 25 が 7.000000000000001 になり 7GB プランの境目を越え、 |
| 12 | + | // FormatFloat は 12.5 を偶数丸めで 12 にする。 |
| 13 | + | |
| 14 | + | func ge(x *big.Rat, k int64) bool { return x.Cmp(big.NewRat(k, 1)) >= 0 } |
| 15 | + | |
| 16 | + | // hu は 0 以上の x を整数に四捨五入する。 |
| 17 | + | func hu(x *big.Rat) int64 { |
| 18 | + | v := new(big.Rat).Add(x, big.NewRat(1, 2)) |
| 19 | + | return new(big.Int).Quo(v.Num(), v.Denom()).Int64() |
| 24 | 20 | | } |
| 25 | 21 | | |
| 26 | | − | func fmtHours(h float64) string { |
| 27 | | − | if h >= 100 { |
| 28 | | − | return fmt.Sprintf("%d 時間", int64(math.Round(h))) |
| 22 | + | func mul(x *big.Rat, k int64) *big.Rat { return new(big.Rat).Mul(x, big.NewRat(k, 1)) } |
| 23 | + | |
| 24 | + | func one(x *big.Rat) string { t := hu(mul(x, 10)); return fmt.Sprintf("%d.%d", t/10, t%10) } |
| 25 | + | |
| 26 | + | func fmtData(gb *big.Rat) string { |
| 27 | + | if ge(gb, 10) { |
| 28 | + | return fmt.Sprintf("%d GB", hu(gb)) |
| 29 | 29 | | } |
| 30 | | − | whole := int64(math.Floor(h)) |
| 31 | | − | mins := int64(math.Round((h - float64(whole)) * 60)) |
| 30 | + | if ge(gb, 1) { |
| 31 | + | return one(gb) + " GB" |
| 32 | + | } |
| 33 | + | mb := mul(gb, 1024) |
| 34 | + | if ge(mb, 100) { |
| 35 | + | return fmt.Sprintf("%d MB", hu(mb)) |
| 36 | + | } |
| 37 | + | return one(mb) + " MB" |
| 38 | + | } |
| 39 | + | |
| 40 | + | func fmtHours(h *big.Rat) string { |
| 41 | + | if ge(h, 100) { |
| 42 | + | return fmt.Sprintf("%d 時間", hu(h)) |
| 43 | + | } |
| 44 | + | whole := new(big.Int).Quo(h.Num(), h.Denom()).Int64() |
| 45 | + | mins := hu(mul(new(big.Rat).Sub(h, big.NewRat(whole, 1)), 60)) |
| 32 | 46 | | if mins == 60 { |
| 33 | 47 | | whole, mins = whole+1, 0 |
| 34 | 48 | | } |
| | | @@ 変更前 41行目〜 / 変更後 55行目〜 @@ |
| 41 | 55 | | return fmt.Sprintf("%d 時間 %d 分", whole, mins) |
| 42 | 56 | | } |
| 43 | 57 | | |
| 44 | | − | func num(s string) float64 { v, _ := strconv.ParseFloat(s, 64); return v } |
| 58 | + | func rat(s string) (*big.Rat, error) { |
| 59 | + | r, ok := new(big.Rat).SetString(s) |
| 60 | + | if !ok { |
| 61 | + | return nil, fmt.Errorf("invalid decimal: %q", s) |
| 62 | + | } |
| 63 | + | return r, nil |
| 64 | + | } |
| 45 | 65 | | |
| 46 | 66 | | // CalcUsage は月の通信量の目安を求める。 |
| 47 | 67 | | func CalcUsage(perHour, hours, days string) (map[string]string, error) { |
| 48 | | − | p, h, d := num(perHour), num(hours), num(days) |
| 49 | | − | monthly := p * h * d |
| 68 | + | p, err := rat(perHour) |
| 69 | + | if err != nil { |
| 70 | + | return nil, err |
| 71 | + | } |
| 72 | + | h, err := rat(hours) |
| 73 | + | if err != nil { |
| 74 | + | return nil, err |
| 75 | + | } |
| 76 | + | d, err := rat(days) |
| 77 | + | if err != nil { |
| 78 | + | return nil, err |
| 79 | + | } |
| 80 | + | perDay := new(big.Rat).Mul(p, h) |
| 81 | + | monthly := new(big.Rat).Mul(perDay, d) |
| 50 | 82 | | plan := "大容量・無制限プランが目安" |
| 51 | 83 | | for _, t := range tiers { |
| 52 | | − | if monthly <= t { |
| 53 | | − | plan = fmt.Sprintf("%dGB プランが目安", int(t)) |
| 84 | + | if monthly.Cmp(big.NewRat(t, 1)) <= 0 { |
| 85 | + | plan = fmt.Sprintf("%dGB プランが目安", t) |
| 54 | 86 | | break |
| 55 | 87 | | } |
| 56 | 88 | | } |
| 57 | | − | return map[string]string{"monthly": fmtData(monthly), "per_day": fmtData(p * h), "hours_3gb": fmtHours(3 / p), "hours_20gb": fmtHours(20 / p), "plan": plan}, nil |
| 89 | + | return map[string]string{"monthly": fmtData(monthly), "per_day": fmtData(perDay), |
| 90 | + | "hours_3gb": fmtHours(new(big.Rat).Quo(big.NewRat(3, 1), p)), "hours_20gb": fmtHours(new(big.Rat).Quo(big.NewRat(20, 1), p)), "plan": plan}, nil |
| 58 | 91 | | } |