google-apps-scriptgoogle-sheetscurrency-formatting

logic in setFormat to not roundup the values


I created a range using offset and setting the format for the range enter image description here

I did that using below code

a1_range = Sheet1.getRange("D43");
var a4_range = a1_range.offset(0,6);
a4_range = a4_range.offset(0,1,15,6);
a4_range.setBackground("#e6e6e6").setBorder(true, true, true, true, true, true);
a4_range.setNumberFormat("$#");

The setNumberFormat is setting is write as I want , but the problem is when user enters the data, it is rounding up the value as shown in the picture.

However, to not round the values, I did this

a4_range.setNumberFormat("$#, ##0.000000");

This works but has extra trailing zeroes which I do not want as below, enter image description here

What I want is when user comes and enters 56.768 in that range, the value should be exactly $56.768 and NOT 56.768000 and NOT have it round off to any decimal places.

Similarly, if the user enters 34.3 then it should be $34.3 and if user enters 8.98945 it should be $8.98945

How do I achieve this?


Solution

  • Try this:

      a4_range.setNumberFormat("$0.#######");