08-02-2022, 10:32 PM
Hi, Lars
This function does what you want:
However, due to a bug in the compiler the MOD operator is not working ok and it will crash. I'm fixing it.
In the meantime, use this other approach:
Note: If you're compiling with --sinclair (compatible mode), remove the -1 in the previous expression.
This function does what you want:
Code:
FUNCTION fmt(num as Float, decimals as UByte) as String
DIM n AS ulong = 10^decimals
dec$ = STR(INT(num * n) MOD n)
RETURN STR(int(num)) + ".000000000000"(0 TO decimals - LEN(dec$)) + dec$
END FUNCTION
PRINT fmt(1.023, 2): REM 2 decimals
PRINT fmt(5.5, 2)
However, due to a bug in the compiler the MOD operator is not working ok and it will crash. I'm fixing it.
In the meantime, use this other approach:
Code:
FUNCTION fmt(num as Float, decimals as UByte) as String
dec$ = STR(INT(num * 10^decimals))
RETURN dec$(TO LEN(dec$) - decimals - 1) + "." + dec$(LEN(dec$) - decimals TO)
END FUNCTION
Note: If you're compiling with --sinclair (compatible mode), remove the -1 in the previous expression.