Skip to content

Commit 07093cb

Browse files
committed
Fix code review issues, and complete formula function implementation
1 parent 31c58a0 commit 07093cb

File tree

1 file changed

+140
-27
lines changed

1 file changed

+140
-27
lines changed

calc.go

Lines changed: 140 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,29 @@ var (
212212
criteriaG,
213213
}
214214
// defines numbers text in the Thai used for the BAHTTEXT formula function.
215-
th0 = "ศูนย์"
216-
th1 = "หนึ่ง"
217-
th2 = "สอง"
218-
th3 = "สาม"
219-
th4 = "สี่"
220-
th5 = "ห้า"
221-
th6 = "หก"
222-
th7 = "เจ็ด"
223-
th8 = "แปด"
224-
th9 = "เก้า"
225-
)
215+
th0 = "\u0E28\u0E39\u0E19\u0E22\u0E4C"
216+
th1 = "\u0E2B\u0E19\u0E36\u0E48\u0E07"
217+
th2 = "\u0E2A\u0E2D\u0E07"
218+
th3 = "\u0E2A\u0E32\u0E21"
219+
th4 = "\u0E2A\u0E35\u0E48"
220+
th5 = "\u0E2B\u0E49\u0E32"
221+
th6 = "\u0E2B\u0E01"
222+
th7 = "\u0E40\u0E08\u0E47\u0E14"
223+
th8 = "\u0E41\u0E1B\u0E14"
224+
th9 = "\u0E40\u0E01\u0E49\u0E32"
225+
th10 = "\u0E2A\u0E34\u0E1A"
226+
th11 = "\u0E40\u0E2D\u0E47\u0E14"
227+
th20 = "\u0E22\u0E35\u0E48"
228+
th1e2 = "\u0E23\u0E49\u0E2D\u0E22"
229+
th1e3 = "\u0E1E\u0E31\u0E19"
230+
th1e4 = "\u0E2B\u0E21\u0E37\u0E48\u0E19"
231+
th1e5 = "\u0E41\u0E2A\u0E19"
232+
th1e6 = "\u0E25\u0E49\u0E32\u0E19"
233+
thDot0 = "\u0E16\u0E49\u0E27\u0E19"
234+
thBaht = "\u0E1A\u0E32\u0E17"
235+
thSatang = "\u0E2A\u0E15\u0E32\u0E07\u0E04\u0E4C"
236+
thMinus = "\u0E25\u0E1A"
237+
)
226238

227239
// calcContext defines the formula execution context.
228240
type calcContext struct {
@@ -1831,22 +1843,6 @@ func formulaCriteriaEval(val formulaArg, criteria *formulaCriteria) (result bool
18311843

18321844
// Engineering Functions
18331845

1834-
// BAHTTEXT function converts a number into Thai text, with the suffix "Baht".
1835-
// The syntax of the function is:
1836-
//
1837-
// BAHTTEXT(number)
1838-
func (fn *formulaFuncs) BAHTTEXT(argsList *list.List) formulaArg {
1839-
if argsList.Len() != 1 {
1840-
return newErrorFormulaArg(formulaErrorVALUE, "BAHTTEXT requires 1 numeric argument")
1841-
}
1842-
token := argsList.Front().Value.(formulaArg)
1843-
number := token.ToNumber()
1844-
if number.Type != ArgNumber {
1845-
return newErrorFormulaArg(formulaErrorVALUE, number.Error)
1846-
}
1847-
return newStringFormulaArg(text)
1848-
}
1849-
18501846
// BESSELI function the modified Bessel function, which is equivalent to the
18511847
// Bessel function evaluated for purely imaginary arguments. The syntax of
18521848
// the Besseli function is:
@@ -13560,6 +13556,123 @@ func (fn *formulaFuncs) ARRAYTOTEXT(argsList *list.List) formulaArg {
1356013556
return newStringFormulaArg(strings.Join(text, ", "))
1356113557
}
1356213558

13559+
// splitBlock function split baht and satang
13560+
func splitBlock(val, size float64) (float64, int) {
13561+
integer, frac := math.Modf((val + 0.1) / size)
13562+
frac = frac*size + 0.1
13563+
return integer, int(frac)
13564+
}
13565+
13566+
13567+
// bahttextAppendDigit appends a digit to the passed string.
13568+
func bahttextAppendDigit(text string, digit int) string {
13569+
if 0 <= digit && digit <= 9 {
13570+
return text + []string{th0, th1, th2, th3, th4, th5, th6, th7, th8, th9}[digit]
13571+
}
13572+
return text
13573+
}
13574+
13575+
// bahttextAppendPow10 appends a value raised to a power of 10: digit*10^pow10.
13576+
func bahttextAppendPow10(text string, digit, pow10 int) string {
13577+
text = bahttextAppendDigit(text, digit)
13578+
switch pow10 {
13579+
case 2:
13580+
text += th1e2
13581+
case 3:
13582+
text += th1e3
13583+
case 4:
13584+
text += th1e4
13585+
case 5:
13586+
text += th1e5
13587+
}
13588+
return text
13589+
}
13590+
13591+
// bahttextAppendBlock appends a block of 6 digits to the passed string.
13592+
func bahttextAppendBlock(text string, val int) string {
13593+
if val >= 100000 {
13594+
text = bahttextAppendPow10(text, val/100000, 5)
13595+
val %= 100000
13596+
}
13597+
if val >= 10000 {
13598+
text = bahttextAppendPow10(text, val/10000, 4)
13599+
val %= 10000
13600+
}
13601+
if val >= 1000 {
13602+
text = bahttextAppendPow10(text, val/1000, 3)
13603+
val %= 1000
13604+
}
13605+
if val >= 100 {
13606+
text = bahttextAppendPow10(text, val/100, 2)
13607+
val %= 100
13608+
}
13609+
if val > 0 {
13610+
n10 := val / 10
13611+
n1 := val % 10
13612+
if n10 >= 1 {
13613+
if n10 >= 3 {
13614+
text = bahttextAppendDigit(text, n10)
13615+
} else if n10 == 2 {
13616+
text += th20
13617+
}
13618+
text += th10
13619+
}
13620+
if n10 > 0 && n1 == 1 {
13621+
text += th11
13622+
} else if n1 > 0 {
13623+
text = bahttextAppendDigit(text, n1)
13624+
}
13625+
}
13626+
return text
13627+
}
13628+
13629+
// BAHTTEXT function converts a number into Thai text, with the suffix "Baht".
13630+
// The syntax of the function is:
13631+
//
13632+
// BAHTTEXT(number)
13633+
func (fn *formulaFuncs) BAHTTEXT(argsList *list.List) formulaArg {
13634+
if argsList.Len() != 1 {
13635+
return newErrorFormulaArg(formulaErrorVALUE, "BAHTTEXT requires 1 numeric argument")
13636+
}
13637+
token := argsList.Front().Value.(formulaArg)
13638+
number := token.ToNumber()
13639+
if number.Type != ArgNumber {
13640+
return newErrorFormulaArg(formulaErrorVALUE, number.Error)
13641+
}
13642+
minus := number.Number < 0
13643+
num := math.Floor(math.Abs(number.Number)*100 + 0.5)
13644+
baht, satang := splitBlock(num, 100)
13645+
var text string
13646+
if baht == 0 {
13647+
if satang == 0 {
13648+
text += th0
13649+
}
13650+
} else {
13651+
for baht > 0 {
13652+
var block string
13653+
var nBlock int
13654+
baht, nBlock = splitBlock(baht, 1.0e6)
13655+
block = bahttextAppendBlock(block, nBlock)
13656+
if baht > 0 {
13657+
block = th1e6 + block
13658+
}
13659+
text = block + text
13660+
}
13661+
}
13662+
if len(text) > 0 {
13663+
text += thBaht
13664+
}
13665+
if satang == 0 {
13666+
text += thDot0
13667+
} else {
13668+
text += thSatang
13669+
}
13670+
if minus {
13671+
text = thMinus + text
13672+
}
13673+
return newStringFormulaArg(text)
13674+
}
13675+
1356313676
// CHAR function returns the character relating to a supplied character set
1356413677
// number (from 1 to 255). The syntax of the function is:
1356513678
//

0 commit comments

Comments
 (0)