cutting extra decimals

This commit is contained in:
Radu Ioan Fericean
2013-12-20 14:11:24 +02:00
parent d8977fc504
commit 08be8d1bb5
4 changed files with 17 additions and 10 deletions

View File

@@ -499,7 +499,8 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) {
cost := 0.0
// re-calculate call cost after balances
for _, ts := range cc.Timespans {
cost += ts.getCost() // FIXME: floating point sum??
cost += ts.getCost()
cost = utils.Round(cost, roundingDecimals, utils.ROUNDING_MIDDLE) // just get rid of the extra decimals
}
cc.Cost = cost
}

View File

@@ -239,8 +239,8 @@ func (i *RateInterval) Equal(o *RateInterval) bool {
func (i *RateInterval) GetCost(duration, startSecond time.Duration) float64 {
price, _, rateUnit := i.
GetRateParameters(startSecond)
d := duration.Seconds()
price /= rateUnit.Seconds()
d := duration.Seconds()
return d * price
}

View File

@@ -21,7 +21,6 @@ package engine
import (
//"fmt"
"log"
"time"
"github.com/cgrates/cgrates/utils"
@@ -215,12 +214,7 @@ func (ts *TimeSpan) getCost() float64 {
ts.Cost = utils.Round(cost, ts.RateInterval.Rating.RoundingDecimals, ts.RateInterval.Rating.RoundingMethod)
return ts.Cost
} else {
cost := 0.0
for _, inc := range ts.Increments {
cost += inc.Cost
log.Print(inc.Cost, cost)
}
return cost
return ts.Increments[0].Cost * float64(len(ts.Increments))
}
return 0
}
@@ -237,6 +231,7 @@ func (ts *TimeSpan) createIncrementsSlice() {
//incrementCost := rate / rateUnit.Seconds() * rateIncrement.Seconds()
nbIncrements := int(ts.GetDuration() / rateIncrement)
incrementCost := ts.getCost() / float64(nbIncrements)
incrementCost = utils.Round(incrementCost, roundingDecimals, utils.ROUNDING_MIDDLE) // just get rid of the extra decimals
for s := 0; s < nbIncrements; s++ {
inc := &Increment{
Duration: rateIncrement,

View File

@@ -228,6 +228,17 @@ func TestTimespanGetCost(t *testing.T) {
}
}
func TestTimespanGetCostIntervals(t *testing.T) {
ts := &TimeSpan{}
ts.Increments = make(Increments, 11)
for i := 0; i < 11; i++ {
ts.Increments[i] = &Increment{Cost: 0.02}
}
if ts.getCost() != 0.22 {
t.Error("Error caclulating timspan cost: ", ts.getCost())
}
}
func TestSetRateInterval(t *testing.T) {
i1 := &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{0, 1.0, 1 * time.Second, 1 * time.Second}}}}
ts1 := TimeSpan{RateInterval: i1}
@@ -718,7 +729,7 @@ func TestTimespanCreateIncrements(t *testing.T) {
if len(ts.Increments) != 3 {
t.Error("Error creating increment slice: ", len(ts.Increments))
}
if len(ts.Increments) < 3 || ts.Increments[2].Cost != 20.066666666666666 {
if len(ts.Increments) < 3 || ts.Increments[2].Cost != 20.0667 {
t.Error("Wrong second slice: ", ts.Increments[2].Cost)
}
}