Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set Number Layout to xx.00?
#1
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
Reply
#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


Forum Jump:


Users browsing this thread: 1 Guest(s)