math.Round
Vladimir Bauer
8 years ago
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | var str string |
| 272 | timeRemaining := time.Duration(float64(st.Total-st.Current) * s.Value()) | |
| 272 | timeRemaining := time.Duration(st.Total-st.Current) * time.Duration(math.Round(s.Value())) | |
| 273 | 273 | hours := int64((timeRemaining / time.Hour) % 60) |
| 274 | 274 | minutes := int64((timeRemaining / time.Minute) % 60) |
| 275 | 275 | seconds := int64((timeRemaining / time.Second) % 60) |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | // CalcPercentage is a helper function, to calculate percentage. |
| 347 | func CalcPercentage(total, current, width int64) (perc int64) { | |
| 347 | func CalcPercentage(total, current, width int64) int64 { | |
| 348 | 348 | if total <= 0 { |
| 349 | 349 | return 0 |
| 350 | 350 | } |
| 352 | 352 | current = total |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | num := float64(width) * float64(current) / float64(total) | |
| 356 | ceil := math.Ceil(num) | |
| 357 | diff := ceil - num | |
| 358 | // num = 2.34 will return 2 | |
| 359 | // num = 2.44 will return 3 | |
| 360 | if math.Max(diff, 0.6) == diff { | |
| 361 | return int64(num) | |
| 362 | } | |
| 363 | return int64(ceil) | |
| 355 | p := float64(width) * float64(current) / float64(total) | |
| 356 | return int64(math.Round(p)) | |
| 364 | 357 | } |
| 365 | 358 | |
| 366 | 359 | // SpeedNoUnit returns raw I/O operation speed decorator. |