I’ve created a couple of functions that should give you the results you need.
<#
Returns the index of the first occurrence of SearchChar in Str.
Fails if SearchChar isn't found.
The function must be <transacts> for now to mutate local data (FoundIndex).
#>
FindCharIndex(SearchChar:char, Str:[]char)<transacts><decides>:int=
var FoundIndex:int = 0
var Found:logic = false
for (X -> Char : Str, not Found?):
if (Char = SearchChar):
set FoundIndex = X
FoundIndex
<#
Returns the smaller of two int values
Verse only provides Min(:float, :float) for now.
#>
Min(X:int, Y:int)<computes>:int=
if (X < Y) then X else Y
# Returns Number converted to a string, truncated to Decimals places.
ToTruncatedString(Number:float, Decimals:int):[]char=
var Str:[]char = ToString(Number)
if:
DotIndex := FindCharIndex['.', Str]
StopIndex := if (Decimals > 0) then Min(DotIndex+Decimals+1,Str.Length) else DotIndex
set Str = Str.Slice[0, StopIndex]
Str
You’d use ToTruncatedString(MyFloat, MyDecimalPlaces)
as your string to print.
Please keep in mind that this isn’t the most performant way to do this, the string traversal in FindCharIndex
is not optimal due to a workaround that’s needed for now.
Also in the future, we’ll most likely provide a different approach that doesn’t require parsing the string character by character.
Hopefully, this works for you. Feel free to ask any questions about the code