Ask Your Question

LO Calc Basic (Macro): How to work with integers

asked 2017-01-07 18:17:18 +0200

Escain gravatar image

updated 2017-01-07 21:12:48 +0200


I am trying to write a very simple function equivalent to this C/C++/Java/..., but in LO Calc Basic

int test( int v) { return v / 10; }

// test(3) -> 0
// test(6) -> 0
// test(14) -> 1
// test (18) -> 1
// test(22) -> 2

The problem is, Calc seem to Ceil or Round integer operations instead of the expected flooring.

function test( v as Long) as Long
    test = v/10
end function 

// test(3) -> 1
// test(6) -> 1

I also tried to create a floor function (Math.Floor is missing for any reason in LO) but does not help:

function floor( v as Double ) as long
    floor = v-(v Mod 1)
end function

function test( v as Long) as Long
    test = floor(v/10)
end function

This unfortunately and without rational, return also the ceil value.

EDITED: Ok, it seem my "floor" function is hidden by the Calc FLOOR function. So I still don't know how to write a working Floor function. Why Calc don't have just the normal Math.floor()?

Thanks for any help.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-01-07 20:48:11 +0200

Escain gravatar image

Ok, after some more research, I found a trick to have a "floor" function and making this code:

function Int( v as Double ) as Long
dim aux as Long
aux = v
if aux > v then 
    Int = aux-1
    Int = aux
end function

function test ( v as Double ) as Long
    test = Int( v / 10)
end function
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-01-07 18:17:18 +0200

Seen: 366 times

Last updated: Jan 07 '17