Skip to content

Commit

Permalink
Merge pull request #505 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 13.5.1
  • Loading branch information
andyone authored Sep 16, 2024
2 parents 8afd234 + 56954b6 commit 398f2bf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### [13.5.1](https://kaos.sh/ek/13.5.1)

- `[mathutil]` Added method `FromPerc`
- `[mathutil]` Code refactoring

### [13.5.0](https://kaos.sh/ek/13.5.0)

- `[support/resources]` Added package for collecting info about CPU and memory
Expand Down
6 changes: 6 additions & 0 deletions mathutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func ExamplePerc() {
// 30%
}

func ExampleFromPerc() {
fmt.Printf("%g\n", FromPerc(15.0, 2860))
// Output:
// 429
}

func ExampleRound() {
fmt.Println(Round(3.14159, 2))
// Output:
Expand Down
15 changes: 12 additions & 3 deletions mathutil/mathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,21 @@ func Abs[N NumericNeg](val N) N {
}

// Perc calculates percentage
func Perc[N Numeric](val1, val2 N) float64 {
if val2 == 0 {
func Perc[N Numeric](current, total N) float64 {
if current == 0 || total == 0 {
return 0
}

return float64(val1) / float64(val2) * 100.0
return (float64(current) / float64(total)) * 100.0
}

// FromPerc calculates value from percentage
func FromPerc(perc float64, total float64) float64 {
if perc <= 0 || total == 0 {
return 0
}

return (total / 100.0) * perc
}

// Round returns rounded value
Expand Down
8 changes: 8 additions & 0 deletions mathutil/mathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ func (s *MathUtilSuite) TestPerc(c *C) {
c.Assert(Perc(100, 100), Equals, 100.0)
c.Assert(Perc(200, 100), Equals, 200.0)
}

func (s *MathUtilSuite) TestFromPerc(c *C) {
c.Assert(FromPerc(0, 0), Equals, 0.0)
c.Assert(FromPerc(100, 0), Equals, 0.0)
c.Assert(FromPerc(-1, 1000), Equals, 0.0)
c.Assert(FromPerc(250, 100), Equals, 250.0)
c.Assert(FromPerc(25.55, -100), Equals, -25.55)
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.5.0"
const VERSION = "13.5.1"

0 comments on commit 398f2bf

Please sign in to comment.