Skip to content

Commit 02189fb

Browse files
authored
Ref qax-os#65, new formula function DOLLAR (qax-os#1992)
- Update unit tests
1 parent ad85417 commit 02189fb

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

calc.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ type formulaFuncs struct {
478478
// DISC
479479
// DMAX
480480
// DMIN
481+
// DOLLAR
481482
// DOLLARDE
482483
// DOLLARFR
483484
// DPRODUCT
@@ -16341,6 +16342,49 @@ func (fn *formulaFuncs) DISC(argsList *list.List) formulaArg {
1634116342
return fn.discIntrate("DISC", argsList)
1634216343
}
1634316344

16345+
// DOLLAR function rounds a supplied number to a specified number of decimal
16346+
// places and then converts this into a text string with a currency format. The
16347+
// syntax of the function is:
16348+
//
16349+
// DOLLAR(number,[decimals])
16350+
func (fn *formulaFuncs) DOLLAR(argsList *list.List) formulaArg {
16351+
if argsList.Len() == 0 {
16352+
return newErrorFormulaArg(formulaErrorVALUE, "DOLLAR requires at least 1 argument")
16353+
}
16354+
if argsList.Len() > 2 {
16355+
return newErrorFormulaArg(formulaErrorVALUE, "DOLLAR requires 1 or 2 arguments")
16356+
}
16357+
numArg := argsList.Front().Value.(formulaArg)
16358+
n := numArg.ToNumber()
16359+
if n.Type != ArgNumber {
16360+
return n
16361+
}
16362+
decimals, dot, value := 2, ".", numArg.Value()
16363+
if argsList.Len() == 2 {
16364+
d := argsList.Back().Value.(formulaArg).ToNumber()
16365+
if d.Type != ArgNumber {
16366+
return d
16367+
}
16368+
if d.Number < 0 {
16369+
value = strconv.FormatFloat(fn.round(n.Number, d.Number, down), 'f', -1, 64)
16370+
}
16371+
if d.Number >= 128 {
16372+
return newErrorFormulaArg(formulaErrorVALUE, "decimal value should be less than 128")
16373+
}
16374+
if decimals = int(d.Number); decimals < 0 {
16375+
decimals, dot = 0, ""
16376+
}
16377+
}
16378+
symbol := map[CultureName]string{
16379+
CultureNameUnknown: "$",
16380+
CultureNameEnUS: "$",
16381+
CultureNameZhCN: "¥",
16382+
}[fn.f.options.CultureInfo]
16383+
numFmtCode := fmt.Sprintf("%s#,##0%s%s;(%s#,##0%s%s)",
16384+
symbol, dot, strings.Repeat("0", decimals), symbol, dot, strings.Repeat("0", decimals))
16385+
return newStringFormulaArg(format(value, numFmtCode, false, CellTypeNumber, nil))
16386+
}
16387+
1634416388
// DOLLARDE function converts a dollar value in fractional notation, into a
1634516389
// dollar value expressed as a decimal. The syntax of the function is:
1634616390
//

calc_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,16 @@ func TestCalcCellValue(t *testing.T) {
21172117
"=DDB(10000,1000,5,5)": "296",
21182118
// DISC
21192119
"=DISC(\"04/01/2016\",\"03/31/2021\",95,100)": "0.01",
2120+
// DOLLAR
2121+
"=DOLLAR(1234.56)": "$1,234.56",
2122+
"=DOLLAR(1234.56,0)": "$1,235",
2123+
"=DOLLAR(1234.56,1)": "$1,234.6",
2124+
"=DOLLAR(1234.56,2)": "$1,234.56",
2125+
"=DOLLAR(1234.56,3)": "$1,234.560",
2126+
"=DOLLAR(1234.56,-2)": "$1,200",
2127+
"=DOLLAR(1234.56,-3)": "$1,000",
2128+
"=DOLLAR(-1234.56,3)": "($1,234.560)",
2129+
"=DOLLAR(-1234.56,-3)": "($1,000)",
21202130
// DOLLARDE
21212131
"=DOLLARDE(1.01,16)": "1.0625",
21222132
// DOLLARFR
@@ -4250,6 +4260,12 @@ func TestCalcCellValue(t *testing.T) {
42504260
"=DISC(\"04/01/2016\",\"03/31/2021\",0,100)": {"#NUM!", "DISC requires pr > 0"},
42514261
"=DISC(\"04/01/2016\",\"03/31/2021\",95,0)": {"#NUM!", "DISC requires redemption > 0"},
42524262
"=DISC(\"04/01/2016\",\"03/31/2021\",95,100,5)": {"#NUM!", "invalid basis"},
4263+
// DOLLAR
4264+
"DOLLAR()": {"#VALUE!", "DOLLAR requires at least 1 argument"},
4265+
"DOLLAR(0,0,0)": {"#VALUE!", "DOLLAR requires 1 or 2 arguments"},
4266+
"DOLLAR(\"\")": {"#VALUE!", "strconv.ParseFloat: parsing \"\": invalid syntax"},
4267+
"DOLLAR(0,\"\")": {"#VALUE!", "strconv.ParseFloat: parsing \"\": invalid syntax"},
4268+
"DOLLAR(1,200)": {"#VALUE!", "decimal value should be less than 128"},
42534269
// DOLLARDE
42544270
"=DOLLARDE()": {"#VALUE!", "DOLLARDE requires 2 arguments"},
42554271
"=DOLLARDE(\"\",0)": {"#VALUE!", "strconv.ParseFloat: parsing \"\": invalid syntax"},

0 commit comments

Comments
 (0)