Why no native ROUND function?

Hi,
As far as I know, Xojo doesn’t have any native functions for rounding like VB6 “Round”.
So I have to resort to Format but I don’t like it because ti changes to type to strings and it won’t work with negative numbers.

So what do you guys use to round decimals ? Did you have to build your own function ^?
thanks

Check the docs. There is a Round function. What are you trying to do?

I use round:
https://documentation.xojo.com/api/math/round.html#round

1 Like

Plus Ceiling() and Floor()!

3 Likes

all these functions (ceiling, floor, round) return an interger, I need to round to 2 decimals for example.
there are no native functions that do this… what are your alternatives ^

roundedValue = Round(unroundedValue * 10) /10
3 Likes

Can you give more information about this?
Format should work with negative numbers, maybe is your format pattern?

So multiply your number by 100, apply the round/etc function, and divide by 100.

SImples.

1 Like

But a built-in performant function to round to a user specified # of decimals places should be in the framework IMO.

  • Karen
1 Like

Maybe he is confused by Floor/Ceiling, at first this looks weird, and I was bitten by it in the past:

Var a, b, c, d As Double

a = Floor(4.5)     //  4
b = Ceiling(4.5)   //  5
c = Floor(-4.5)    // -5
d = Ceiling(-4.5)  // -4

break

a = Floor(4.4)     //  4
b = Ceiling(4.4)   //  5
c = Floor(-4.4)    // -5
d = Ceiling(-4.4)  // -4

break

a = Floor(4.0)     //  4
b = Ceiling(4.0)   //  4
c = Floor(-4.0)    // -4
d = Ceiling(-4.0)  // -4

break

a = Floor(4.9)     //  4
b = Ceiling(4.9)   //  5
c = Floor(-4.9)    // -5
d = Ceiling(-4.9)  // -4

break

OP mentions format and that it doesn’t work with negative numbers.
My guess is that is doing something like:

Var a As Double = -3.1456
Var b As String = Format(a, "#.##") // b = 3.15

:man_shrugging:t2:

Var a As Double = -3.1456
Var b,c As String
b = Format(a, "-#.##")  // b = -3,15 // locale uses comma, and needs "-"
c = a.ToString("#.##")  // c = -3.15 // US format anyway, don't need "-"

Break