Search code examples
google-sheets

Remove trailing ".00" from numbers in Google Sheets


In Google Sheets I'm trying to format numbers to have format "0.##" - so that integer be like integer (12 be exactly 12), decimals be like decimals with no trailing zeroes (12.30 be 12.3 and 12.4321 be 12.43), but in reality for 12 I've got 12. with this annoying decimal point and see NO WAY to get rid of it.

enter image description here

The same time if I choose Format -> Number -> Automatic, I've got calculated numbers be like 131.3666667 which is not my desired format.

In LibreOffice using format "0.##" removes unnecessary decimal point, but Google Sheets don't. If you know how to do it, please share your knowledge. Googling doesn't help much.

Thank you in advance!


Solution

  • Number format 0.## works well with Google Apps Script. It does not leave dot to numbers that has no decimals.

    The code below will apply number formatting to the whole Sheet.

    Try this:

    function formatColumn() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("Sheet1");
      var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
      range.setNumberFormat("0.##");
    }
    

    *Note:

    • You can change the range to column specific by changing the value of getRange(). Example: sheet.getRange(A1:A).
    • If you choose to format the whole sheet, you only need to run the script once to apply the format.

    Output:

    Before:

    enter image description here

    After:

    enter image description here

    Since we set the format to the whole sheet, It will automatically format any inputted numbers.

    enter image description here

    Reference: