Skip to content

Instantly share code, notes, and snippets.

@MattRix
Last active October 13, 2024 18:54
Show Gist options
  • Save MattRix/d88d9f4ab04504486762141c08fe94cd to your computer and use it in GitHub Desktop.
Save MattRix/d88d9f4ab04504486762141c08fe94cd to your computer and use it in GitHub Desktop.
A function to write a float to a string with a certain numer of decimal places in Verse
FormatRoundedFloatAsString(Input : float, NumDigitsAfterDecimal : int) : string =
if:
Multiplier := Pow(10.0, NumDigitsAfterDecimal * 1.0)
RoundedValue := float[Round[Input * Multiplier] * 1.0] / Multiplier
BeforeDecimal := Floor[RoundedValue]
AfterDecimal := Abs(Round[(RoundedValue - BeforeDecimal * 1.0)*Multiplier])
var AfterDecimalString : string = ToString(AfterDecimal)
#pad the number after the decimal with leading zeroes
for (It := 0..(NumDigitsAfterDecimal - AfterDecimalString.Length - 1)):
set AfterDecimalString = array{'0'} + AfterDecimalString
then:
if(AfterDecimalString.Length > 0):
"{BeforeDecimal}.{AfterDecimalString}"
else:
"{BeforeDecimal}"
else:
ToString(Input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment