Last active
October 13, 2024 18:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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