Set Number Layout to xx.00? - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16) +---- Thread: Set Number Layout to xx.00? (/showthread.php?tid=2235) Set Number Layout to xx.00? - Lars_74 - 07-28-2022 Hi, I have been trying all evening to work on a very easy task. Well, I thought it would be easy... Nothing really worked and the only positive sideffect is that I learned a lot about ZX Basic. I would like to set the number's layout to xx.00 Because it looks nicer. Examples: 1.5 > 1.50 1 > 1.00 .1 > 0.10 The only thing I could come up is to work on two levels. level a: the correct numbers the computer is working with, e.g. 12.012345 level b: the number handed over to a Sub-Routine which switches the number to a string, searches for "." and gives back a formated string to the main routine. Thus: the Spectrum works with level a (number), the user only sees level b (string). Is it really that difficult or is it me being just to stupid...? Lars RE: Set Number Layout to xx.00? - boriel - 08-02-2022 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.