I just found that you can do this with the already existing Format()
function. No need for making your own function and no need for using VBA
compatibility:
Print Format(12.125, "0.00")
The second parameter is what you would have written in the ”Format code”
field when formatting a cell in Calc.
Result: 12.13
Kind regards
Johnny Rosenberg
Den sön 5 juni 2022 kl 11:37 skrev Johnny Rosenberg <gurus.knugum@gmail.com
:
Looks like a bug to me. 12.125 returns 12.12, but 12.1251 returns 12.13.
Both should return the same.
I guess this bug should be reported.
Meanwhile, you can make your own function that you can use. Here are two
different suggestions.
1: This one use the Calc built-in ROUND() cell function. Place it in ”My
macros & dialogs”. VBA compatibility mode is not needed.
Public Function faRound(x As Double, d As Integer)
Dim Calc
Calc=createUnoService("com.sun.star.sheet.FunctionAccess")
faRound=Calc.callFunction("ROUND", Array(x, d))
End Function
2: This one use the method we learned at school:
Public Function fRound(x As Double, d As Integer)
fRound=Int(10^d*x+.5)/10^d
End Function
Test:
Print afRound(12.125, 2)
Result: 12.13
Print fRound(12.125, 2)
Result: 12.13
Print fRound(12.125, 1)
Result: 12.1
… and so on.
Kind regards
Johnny Rosenberg
Den sön 5 juni 2022 kl 04:46 skrev Steve Edmonds <
steve.edmonds@ptglobal.com>:
I have just put a basic macro together so it will calculate income tax
due from our local tax rates.
I needed to round the tax to the nearest cent so taking the easy route I
added Option VBASupport 1 to use the available VB round function.
I then noticed that the VB round function with say round(12.125,2)
rounds down to 12.12 and the LO inbuilt spread sheet function ROUND with
ROUND(12.125,2) rounds up to 12.13.
Is rounding in this situation arbitrary or is there some some convention
for consistency.
Steve
--
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems?
https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy
--
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems? https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy
Context
Privacy Policy |
Impressum (Legal Info) |
Copyright information: Unless otherwise specified, all text and images
on this website are licensed under the
Creative Commons Attribution-Share Alike 3.0 License.
This does not include the source code of LibreOffice, which is
licensed under the Mozilla Public License (
MPLv2).
"LibreOffice" and "The Document Foundation" are
registered trademarks of their corresponding registered owners or are
in actual use as trademarks in one or more countries. Their respective
logos and icons are also subject to international copyright laws. Use
thereof is explained in our
trademark policy.