ref #65, new formula function: EUROCONVERT

This commit is contained in:
xuri 2022-06-22 20:17:22 +08:00
parent 852f211970
commit 61c71caf4f
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 98 additions and 0 deletions

83
calc.go
View File

@ -442,6 +442,7 @@ type formulaFuncs struct {
// ERFC
// ERFC.PRECISE
// ERROR.TYPE
// EUROCONVERT
// EVEN
// EXACT
// EXP
@ -16080,6 +16081,88 @@ func (fn *formulaFuncs) EFFECT(argsList *list.List) formulaArg {
return newNumberFormulaArg(math.Pow(1+rate.Number/npery.Number, npery.Number) - 1)
}
// EUROCONVERT function convert a number to euro or from euro to a
// participating currency. You can also use it to convert a number from one
// participating currency to another by using the euro as an intermediary
// (triangulation). The syntax of the function is:
//
// EUROCONVERT(number,sourcecurrency,targetcurrency[,fullprecision,triangulationprecision])
//
func (fn *formulaFuncs) EUROCONVERT(argsList *list.List) formulaArg {
if argsList.Len() < 3 {
return newErrorFormulaArg(formulaErrorVALUE, "EUROCONVERT requires at least 3 arguments")
}
if argsList.Len() > 5 {
return newErrorFormulaArg(formulaErrorVALUE, "EUROCONVERT allows at most 5 arguments")
}
number := argsList.Front().Value.(formulaArg).ToNumber()
if number.Type != ArgNumber {
return number
}
sourceCurrency := argsList.Front().Next().Value.(formulaArg).Value()
targetCurrency := argsList.Front().Next().Next().Value.(formulaArg).Value()
fullPrec, triangulationPrec := newBoolFormulaArg(false), newNumberFormulaArg(0)
if argsList.Len() >= 4 {
if fullPrec = argsList.Front().Next().Next().Next().Value.(formulaArg).ToBool(); fullPrec.Type != ArgNumber {
return fullPrec
}
}
if argsList.Len() == 5 {
if triangulationPrec = argsList.Back().Value.(formulaArg).ToNumber(); triangulationPrec.Type != ArgNumber {
return triangulationPrec
}
}
convertTable := map[string][]float64{
"EUR": {1.0, 2},
"ATS": {13.7603, 2},
"BEF": {40.3399, 0},
"DEM": {1.95583, 2},
"ESP": {166.386, 0},
"FIM": {5.94573, 2},
"FRF": {6.55957, 2},
"IEP": {0.787564, 2},
"ITL": {1936.27, 0},
"LUF": {40.3399, 0},
"NLG": {2.20371, 2},
"PTE": {200.482, 2},
"GRD": {340.750, 2},
"SIT": {239.640, 2},
"MTL": {0.429300, 2},
"CYP": {0.585274, 2},
"SKK": {30.1260, 2},
"EEK": {15.6466, 2},
"LVL": {0.702804, 2},
"LTL": {3.45280, 2},
}
source, ok := convertTable[sourceCurrency]
if !ok {
return newErrorFormulaArg(formulaErrorVALUE, formulaErrorVALUE)
}
target, ok := convertTable[targetCurrency]
if !ok {
return newErrorFormulaArg(formulaErrorVALUE, formulaErrorVALUE)
}
if sourceCurrency == targetCurrency {
return number
}
var res float64
if sourceCurrency == "EUR" {
res = number.Number * target[0]
} else {
intermediate := number.Number / source[0]
if triangulationPrec.Number != 0 {
ratio := math.Pow(10, triangulationPrec.Number)
intermediate = math.Round(intermediate*ratio) / ratio
}
res = intermediate * target[0]
}
if fullPrec.Number != 1 {
ratio := math.Pow(10, target[1])
res = math.Round(res*ratio) / ratio
}
return newNumberFormulaArg(res)
}
// FV function calculates the Future Value of an investment with periodic
// constant payments and a constant interest rate. The syntax of the function
// is:

View File

@ -1922,6 +1922,13 @@ func TestCalcCellValue(t *testing.T) {
// EFFECT
"=EFFECT(0.1,4)": "0.103812890625",
"=EFFECT(0.025,2)": "0.02515625",
// EUROCONVERT
"=EUROCONVERT(1.47,\"EUR\",\"EUR\")": "1.47",
"=EUROCONVERT(1.47,\"EUR\",\"DEM\")": "2.88",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\")": "0.44",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\",FALSE)": "0.44",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\",FALSE,3)": "0.44",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\",TRUE,3)": "0.43810592",
// FV
"=FV(0.05/12,60,-1000)": "68006.0828408434",
"=FV(0.1/4,16,-2000,0,1)": "39729.4608941662",
@ -3958,6 +3965,14 @@ func TestCalcCellValue(t *testing.T) {
"=EFFECT(0,\"\")": "strconv.ParseFloat: parsing \"\": invalid syntax",
"=EFFECT(0,0)": "#NUM!",
"=EFFECT(1,0)": "#NUM!",
// EUROCONVERT
"=EUROCONVERT()": "EUROCONVERT requires at least 3 arguments",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\",TRUE,3,1)": "EUROCONVERT allows at most 5 arguments",
"=EUROCONVERT(\"\",\"FRF\",\"DEM\",TRUE,3)": "strconv.ParseFloat: parsing \"\": invalid syntax",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\",\"\",3)": "strconv.ParseBool: parsing \"\": invalid syntax",
"=EUROCONVERT(1.47,\"FRF\",\"DEM\",TRUE,\"\")": "strconv.ParseFloat: parsing \"\": invalid syntax",
"=EUROCONVERT(1.47,\"\",\"DEM\")": "#VALUE!",
"=EUROCONVERT(1.47,\"FRF\",\"\",TRUE,3)": "#VALUE!",
// FV
"=FV()": "FV requires at least 3 arguments",
"=FV(0,0,0,0,0,0,0)": "FV allows at most 5 arguments",