Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rounding float numbers to 2 decimal places
#2
There is a trick for that which can be accomplished in several ways. Here's an example of a ROUND function (explained in the ZX Spectrum Manual):

Code:
FUNCTION Round(n as Float, decimals as UByte = 0) AS Float
  DIM tmp as Float
  DIM d10 = 10^decimals
  IF n >= 0 THEN
    LET tmp = INT(n * d10 + 0.5)
  ELSE
    LET tmp = INT(n * d10 - 0.5)
  END IF
  RETURN tmp / d10
END FUNCTION
This function will round towards +/- infinity.
Use with Round(number, digits)

I'll include it in the math library.
Reply


Messages In This Thread
RE: Rounding float numbers to 2 decimal places - by boriel - 12-29-2023, 11:54 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)