[Solved] Round all numeric cells to 2 decimals

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
luofeiyu
Posts: 55
Joined: Thu Sep 14, 2017 2:11 am

[Solved] Round all numeric cells to 2 decimals

Post by luofeiyu »

I write a triple loop to round all cells whose type is number to 2:

Code: Select all

sub roundAllCells
   dim oFunAccess as object
   oFunAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
   for each oSheet in ThisComponent.Sheets
        oCurs = oSheet.createCursor()  
        oCurs.gotoEndOfUsedArea(True)
        num_row = oCurs.Rows.Count
        num_col = oCurs.Columns.Count
        for  i=0 to num_row-1
        for  j=0 to num_col-1
            oCell = oSheet.getCellByPosition(j,i)
            if  oCell.type  = 1 then               
               oCell.value = oFunAccess.CallFunction( "round", array(oCell.value,2))
            end if
        next j
        next i 
   next
end sub
It is so long ,can simplify it with pure starbasic code,instead of setting "CompatibilityMode( true )" to use "round(number,digitals)" in VBA?

 Edit: Changed subject, was How can simplify the function to round all cells whose type is number? 
Make your post understandable by others 
-- MrProgrammer, forum moderator 
Last edited by MrProgrammer on Sun Feb 09, 2025 6:43 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved]
LibreOffice 24.8 on Debian 12
User avatar
MrProgrammer
Moderator
Posts: 5322
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Round all numeric cells to 2 decimals

Post by MrProgrammer »

luofeiyu wrote: Sat Feb 01, 2025 4:09 pm It is so long …
Zero lines of programming are needed when you use Precision as shown. Zero lines of programming require zero hours of research, testing, and debugging. Read about this setting in Help → Index or in User Guides (PDF) or searching for topics about it in the Calc Forum. It is best when you use a style to specify the numeric formats for your cells.
202502010822.gif
202502010822.gif (84.05 KiB) Viewed 8482 times

If this solved your problem please go to your first post use the Edit ✏️ button and add [Solved] to the start of the Subject field. Select the green checkmark icon at the same time.

[Tutorial] Ten concepts that every Calc user should know
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.6, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
karolus
Volunteer
Posts: 1231
Joined: Sat Jul 02, 2011 9:47 am

Re: Round all numeric cells to 2 decimals

Post by karolus »

Code: Select all

def round_to_digits(*_, digits=2):
    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.Sheets[0]
    values = sheet.queryContentCells(1)  # 1 ⇒ any float, except its interpreted as date or return of Formula #
    # use »17« to include also Formularesults, ! be aware the Formulas will be lossed! #
    for cell in values.Cells:
        cell.Formula = round(cell.Value, digits)
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (Bookworm) (on RaspberryPI5)
Bidouille
Volunteer
Posts: 648
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Round all numeric cells to 2 decimals

Post by Bidouille »

Why call the ROUND function?
Use the Format instruction (type F1 key for help)
Locked