Add CalculatePercentageGainOrLoss function

This commit is contained in:
Adrian Gallagher
2017-02-19 18:30:41 +11:00
parent 983e89376b
commit 3de08b76e4
2 changed files with 19 additions and 4 deletions

View File

@@ -175,8 +175,12 @@ func CalculateFee(amount, fee float64) float64 {
return amount * (fee / 100)
}
func CalculatePercentageGainOrLoss(priceNow, priceThen float64) float64 {
return (priceNow - priceThen) / priceThen * 100
}
func CalculatePercentageDifference(amount, secondAmount float64) float64 {
return (secondAmount - amount) / amount * 100
return (amount - secondAmount) / ((amount + secondAmount) / 2) * 100
}
func CalculateNetProfit(amount, priceThen, priceNow, costs float64) float64 {

View File

@@ -183,11 +183,22 @@ func TestCalculateAmountWithFee(t *testing.T) {
}
}
func TestCalculatePercentageGainOrLoss(t *testing.T) {
t.Parallel()
originalInput := float64(9300)
secondInput := float64(9000)
expectedOutput := 3.3333333333333335
actualResult := CalculatePercentageGainOrLoss(originalInput, secondInput)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
}
}
func TestCalculatePercentageDifference(t *testing.T) {
t.Parallel()
originalInput := float64(5)
secondAmount := float64(10)
expectedOutput := float64(100)
originalInput := float64(10)
secondAmount := float64(5)
expectedOutput := 66.66666666666666
actualResult := CalculatePercentageDifference(originalInput, secondAmount)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))