Microsoft Small Basic

Program Listing: DWM520
' Write with Format 0.1
debug = "False"
param = "0=#,##0.000;1=12.34;"
WriteFormat()
TextWindow.WriteLine("")
param = "0=0.0;1=-12.34;"
WriteFormat()
TextWindow.WriteLine("")
Sub WriteFormat
CheckParam()
If debug Then
DumpParam()
EndIf
p = 1
q = 1
len = Text.GetLength(format)
txt = ""
While p <= len
f = Text.GetSubText(format, p, 1)
If f = "#" Then
w = Text.GetSubText(num, q, 1)
q = q + 1
If w = 0 Then
w = " "
EndIf
txt = Text.Append(txt, w)
ElseIf f = "0" Then
w = Text.GetSubText(num, q, 1)
q = q + 1
txt = Text.Append(txt, w)
ElseIf f = "," Then
If w = " " Then
txt = Text.Append(txt, w)
Else
txt = Text.Append(txt, f)
EndIf
ElseIf f = "." Then
q = q + 1
txt = Text.Append(txt, f)
Else
txt = Text.Append(txt, f)
EndIf
p = p + 1
EndWhile
If sign <> "" Then
p = 1
While Text.GetSubText(txt, p, 1) = " "
p = p + 1
EndWhile
If p = 1 Then
txt = Text.Append(sign, txt)
Else
t1 = Text.GetSubText(txt, 1, p - 2)
t2 = Text.GetSubTextToEnd(txt, p)
txt = Text.Append(t1, sign)
txt = Text.Append(txt, t2)
EndIf
EndIf
TextWindow.Write(txt)
EndSub
Sub CheckParam
p = 1
format = param[0]
len = Text.GetLength(format)
nInt = 0
nFrac = 0
hasDot = "False"
While p <= len
f = Text.GetSubText(format, p, 1)
If f = "#" Then
nInt = nInt + 1
ElseIf f = "0" Then
If hasDot Then
nFrac = nFrac + 1
Else
nInt = nInt + 1
EndIf
ElseIf f = "." Then
hasDot = "True"
Else
EndIf
p = p + 1
EndWhile
If param[1] < 0 Then
sign = "-"
Else
sign = ""
EndIf
num = Math.Abs(param[1])
dot = Text.GetIndexOf(num, ".")
If dot > 0 Then
len = dot - 1
Else
len = Text.GetLength(num)
num = Text.Append(num, ".")
dot = Text.GetLength(num)
EndIf
For i = 1 To nInt - len
num = Text.Append("0", num)
dot = dot + 1
EndFor
add = (dot - 1) - nInt
For i = 1 To add
format = Text.Append("#", format)
EndFor
For i = 1 To nFrac - (Text.GetLength(num) - dot)
num = Text.Append(num, "0")
EndFor
EndSub
Sub DumpParam
TextWindow.ForegroundColor = "Green"
TextWindow.WriteLine("param[0]=" + param[0])
TextWindow.WriteLine("param[1]=" + param[1])
TextWindow.WriteLine("format=" + format)
TextWindow.WriteLine("sign=" + sign)
TextWindow.WriteLine("num=" + num)
TextWindow.WriteLine("nInt=" + nInt)
TextWindow.WriteLine("nFrac=" + nFrac)
TextWindow.WriteLine("hasDot=" + hasDot)
TextWindow.WriteLine("dot=" + dot)
TextWindow.WriteLine("add=" + add)
TextWindow.ForegroundColor = "Gray"
EndSub