KDFZ
KDFZ

Reputation: 31

Change # of decimal places with Google app script?

Is it possible to change # of decimal places of all cells in Google Sheets with app script? I'm trying to create a dropdown button in Google Sheets where the user can select the # of decimal places to display in current worksheet. I've checked the app script documentation but didn't find anything regarding this.

Upvotes: 2

Views: 16303

Answers (3)

Neil Pickles
Neil Pickles

Reputation: 11

You can use the setNumberFormat() function within the Range object.

https://developers.google.com/apps-script/reference/spreadsheet/range#setnumberformatnumberformat

SpreadsheetApp.getActiveSheet().getRange("A1").setNumberFormat("0.00")

Upvotes: 1

J-su-35
J-su-35

Reputation: 61

.toFixed() Method may help your work.

var x = 9.656;

x.toFixed(2);

Number Methods

Upvotes: 6

MasterCrander
MasterCrander

Reputation: 476

I am not aware of a Google Sheets formula to allow for this. There are also not GAS specific methods to perform the function as far as I have seen. There are, however, some universal JavaScript methods to mimic this.

You can find the documentation for the .toFixed() method here. This method changes a number to a string with the specified decimal places. This will change the output format so be aware of the type of data you are working with.

Upvotes: 1

Related Questions