Remove trailing ".00" from numbers in Google Sheets - 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.
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!

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:
After:
Since we set the format to the whole sheet, It will automatically format any inputted numbers.
Reference:
Google Apps Script
Class Range
Range.setNumberFormat(numberFormat)

hi i found the Solution :
For Example let us say that you have the number 100 in the cell A1 ,
and you have the Number 99.9 in the cell B1 .
let us say you want to get the sum of them without having the.9 .
click on the cell that you want to add the sum on it :
let us say it is the cell C1 click on it then add =INT //(MUST BE CAPITAL) :
for example :
= INT(A1 + B1)
then hit Enter .
now the value of the sum is :
199

If you don't use the result of these cells for further calculation (or if you do use, but don't mind losing some precision), you can surround your current formula with Round(..., 2) and leave the format as automatic.
Some examples of what original numbers will appear as:
1.004 -> 1
1.006 -> 1.01
1.096 -> 1.1

Related

How to calculate the difference of numbers in series using Google sheets formula?

I have an inventory sheet, where I need to calculate the difference between the actual value and the series of expected values. Here is a snapshot of what I am talking about:
So for Item1:
Current: 50
Expected: 45,45,45
Now I want to devise a formula that will calculate a difference until the last column in this specific row, something like this:
difference = 50-(45,45,45) //50-45+50-45+50-45 and so on (Q,R..)
difference = 15 //final result
Is there any way that we can achieve this by putting formula in a cell, any help would be much appreciated, thank you.
In google-sheet you can use-
=ArrayFormula(SUM(M2-N2:P2))
In excel use below formula
=SUM(M2-N2:P2)
In case of non 365 version of excel you need to enter it as array means confirm by CTRL+SHIFT+ENTER.

How to SPLIT cell content into sets of 50000 characters in new columns Google Sheets

I have 3 columns A, B & C as shown in the image. Column A contains the search key. The second column B contains names and their respective content in the third column C.
I am filtering rows that contain the text in A1 in B:C and concatenating them. The challenge is that each text in the third column is roughly 40k characters. The filter formula works well so the issue is the character limit. This formula =ArrayFormula(query(C1:C,,100000)) which I have in F1 concatenates more than 50000 characters but I am not how to apply it for my case.
Tried to wrap my formula in E1 inside the query function but it wasn't successful. Like so:
=ArrayFormula(query(CLEAN(CONCATENATE(FILTER(C1:C, B1:B=A1))),,100000)).
I also tried to SPLIT the concatenated result into sets of 50000 characters and put the extras in the next columns but wouldn't manage either. The formula I tried in this case is:
=SPLIT(REGEXREPLACE(CLEAN(CONCATENATE(FILTER(C1:C, B1:B=A1))),".{50000}", "$0,"),",")
The link to the spreadsheet
https://docs.google.com/spreadsheets/d/1rhVSQJBGaPQu6y2WbqkO2_UqzyfCc3_76t4AK3PdF7M/edit?usp=sharing
Since cell is limited to 50,000 characters, using CONCATENATE is not possible. Alternative solution is to use Google Apps Script's custom function. The good thing about Apps Script is it can handle millions of string characters.
To create custom function:
Create or open a spreadsheet in Google Sheets.
Select the menu item Tools > Script editor.
Delete any code in the script editor and copy and paste the code below.
At the top, click Save.
To use custom function:
Click the cell where you want to use the function.
Type an equals sign (=) followed by the function name and any input value — for example, =myFunction(A1) — and press Enter.
The cell will momentarily display Loading..., then return the result.
Code:
function myFunction(text) {
var arr = text.flat();
var newStr = arr.join(' ');
var slicedStr = stringChop(newStr, 50000);
return [slicedStr];
}
function stringChop(str, size){
if (str == null) return [];
str = String(str);
size = ~~size;
return size > 0 ? str.match(new RegExp('.{1,' + size + '}', 'g')) : [str];
}
Example:
Based on your sample spreadsheet, there are 4 rows that matches the criteria of the filter and each cell contains 38,976 characters, which is 155,904 characters in total. Dividing it by 50,000 is 3.12. The ceiling of 3.12 is 4 which means we have 4 columns of data.
Usage:
Paste this in cell E1:
=myFunction(FILTER(C1:C, B1:B=A1))
Output:
Reference:
Custom Function

Using number formatting on a scientific number changes the value

I have a string which is eval_id = -8880305704784521238 in google sheet.
When I used split formula =SPLIT(A1,"=") it gives me result in scientific number
eval_id -8.88031E+18
But when I used number formatting it changes the value of the number.-8880305704784520000 which is not equal to the actual number -8880305704784521238.
How can I fix this issue.
Try this:
=REGEXEXTRACT(A1,"-*\d*.?\d+")
or as an array formula (specify the range):
=ARRAYFORMULA(REGEXEXTRACT(A1:A15,"-*\d*.?\d+"))

Different Outputs while counting unique values in a Google Sheet

I have a google sheet which looks like this :
The formula for cell M3 is =COUNTA(UNIQUE(B3:L3)) which outputs the 01/10/00. However the cell B18 is =COUNTA(UNIQUE(B3:B17)) and its output is 15
I wanted to get this count of unique values in the range using the formula but can't figure out the cause of difference of the outputs. Also, the count of unique values in a row should be 11 which is not really reflected in M3 and any changes are not changing the value of the output either.
there is a combo formula for that called COUNTUNIQUE
=COUNTUNIQUE(B3:L3)
The formatting of M3 is probably set to Date and that's converting your number into that date view. Additionally, if you're getting a higher number than you expect in general, double check for trailing spaces that can trick the unique() function.

Google App Script: Convert Currency, but keep value from moment of conversion

I've built myself a rather fancy finance tracking spreadsheet, but have run into the following problem:
I track my expenses with two values: amount and currency.
Then, in the next column, I use GOOGLEFINANCE() to convert those values to my native currency (Euro).
My formula looks like this:
IF(B2 = "EUR"; A2; A2*GOOGLEFINANCE("CURRENCY:" & B2 &"EUR"))
If this row's currency (B column) is EUR, don't do any conversion and just use the value from the A column.
If B is not EUR, take the currency in B and convert the monetary value in A from that currency into EUR.
The problem is that this is constantly updating. In the moment this is okay, but if I go back to it in a few years some values might be very different from the original exchange rate.
What I would like to do is to fill out the field with today's exchange rate and leave the value like that (i.e. static, not dynamic).
Does anyone have any experience with this? I guess it'd also be a common problem for building stock-price record sheets.
I also have a lot of finance tracking sheets and you are right, freezing a returned value is a common need. The below script is set to run onEdit of cell B2. (You could change it for column B). It get cell C2 using offset. It gets the display value of the formula result of your formula calculation and overwrites the formula with the value. Since the formula is gone, it won't recalculate.
function onEdit(e) {
if(e.range.getA1Notation()=="B2"){
freeze(e.range.getA1Notation());
}}
function freeze(c){
var ss =SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet()
var activeCell =c
var oc =SpreadsheetApp.getActiveRange().offset(0, 1).getA1Notation()
var val=s.getRange(oc).getDisplayValue()
s.getRange(oc).setValue(val);
}
Here is an improved version to check the sheet and handle all of column B, not just B2. Change the formula in B2 to this and copy down:
=IF(or(B2 = "EUR",B2=""), A2, A2*GOOGLEFINANCE("CURRENCY:" & B2 &"EUR"))
Then change onEdit to this. Change the sheet name as needed:
function onEdit(e){
var sheet = e.source.getActiveSheet().getName()
var c = e.range.getA1Notation()
var col=e.range.getColumn()
var lcval=e.value.toLowerCase()//to lower case to ignore case.
if(lcval==""){return}
if(sheet=="Sheet1" && col==2 && lcval != "eur"){ //Change sheet name as needed.
freeze(c)//call freeze wuth active cell address.
}
}

Resources