Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set Number Layout to xx.00?
#2
Hi, Lars

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.
Reply


Messages In This Thread
Set Number Layout to xx.00? - by Lars_74 - 07-28-2022, 11:11 PM
RE: Set Number Layout to xx.00? - by boriel - 08-02-2022, 10:32 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)