Support Forums > WinFBX - Windows Framework for FreeBASIC

Rounding

(1/1)

Josť Roca:
Hi Johan Klassen (aka srvaldez)

Re: https://www.freebasic.net/forum/viewtopic.php?f=7&t=27198

You can use:


--- Code: ---' ========================================================================================
FUNCTION AfxRound (BYVAL dblValue AS DOUBLE, BYVAL numDigits AS LONG) AS DOUBLE
   IF numDigits < 1 THEN RETURN dblValue
   IF numDigits > 9 THEN numDigits = 9
   DIM nMult AS LONG = 10 ^ numDigits
   FUNCTION = FIX((nMult * dblValue + (SGN(dblValue)) * .5)) / nMult
END FUNCTION
' ========================================================================================

' ========================================================================================
FUNCTION AfxRoundToStr (BYVAL dblValue AS DOUBLE, BYVAL numDigits AS LONG) AS STRING
   IF numDigits < 1 THEN RETURN STR(dblValue)
   IF numDigits > 9 THEN numDigits = 9
   DIM nMult AS LONG = 10 ^ numDigits
   dblValue = FIX((nMult * dblValue + (SGN(dblValue)) * .5)) / nMult
   DIM strNum AS STRING = STR(dblValue)
   DIM p AS LONG = INSTRREV(strNum, ".")
   DIM nLen AS LONG = LEN(MID(strNum, p + 1))
   IF p THEN
      IF nLen < numDigits THEN strNum += STRING(numDigits - nLen, "0")
   ELSE
      strNum += "." & STRING(numDigits, "0")
   END IF
   FUNCTION = strNum
END FUNCTION
' ========================================================================================

--- End code ---

Use as you wish. No restrictions :)

Navigation

[0] Message Index

Go to full version