[Solved] BASIC Precision rounding function

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

[Solved] BASIC Precision rounding function

Post by Evgeniy »

Is there a ready-made function in basic for rounding with a certain accuracy?

For example "22.6543257567869"->"22.65"

I think it can be done like this:

Code: Select all

outputvalue=CDbl(CInt(inputvalue*100))/100
But can there be other better ways?
Last edited by Evgeniy on Sat Jan 11, 2020 10:27 pm, edited 1 time in total.
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: BASIC Precision rounding function

Post by JeJe »

You can use functionaccess and Calc's Round function

https://wiki.openoffice.org/wiki/Docume ... D_function

Code: Select all

fc=createUnoService("com.sun.star.sheet.FunctionAccess")
msgbox fc.callFunction("Round",Array(22.6543257567869,2))
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: BASIC Precision rounding function

Post by Evgeniy »

Thanks, not bad.

I'm writing simple function:

Code: Select all

Function Round(val As Double,digits_after_point As Integer) As Double
	Round = CDbl(CLng(val*(10^digits_after_point)))/(10^digits_after_point)
End Function
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: BASIC Precision rounding function

Post by JeJe »

Okay... but there's little point in reinventing the wheel IMO...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: [Solved] BASIC Precision rounding function

Post by Evgeniy »

Sometimes it is useful for improve logic skills :)
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
Post Reply