简体   繁体   中英

Google Sheet Script Editor - how would one format their numbers properly?

So I`m trying to format my number to include percentage sign using script editor

That "valuetoCheck" is the target i`m trying to change

I am sending these emails to my colleagues when error rate is greater than 10 %

That 'J1' is basically extracted from the sheet by using formula (=max(C:C)) and they are already in percent format in actual google sheet itself

How should I format that J1 so that it can be shown with proper percent sign in the email?

The current code generates the below message in the email..

Reported Maximum default rate is: 0.020380774742397016

and the number is supposed to be 2% if it was done properly

function checkValue()

{

var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Final_Report');
var url = ss.getUrl();
var valueToCheck = sheet.getRange('J1').getValue();
var emailSendTo = 'random@hotmail.com'
var subject = "Errpr - Detected!";
var message = "Please check Error rates \n\n Reported Maximum Error rate is: " + valueToCheck 

if(valueToCheck > 0.1)

  {

MailApp.sendEmail(emailSendTo, subject, message);
}
}

Regardless of how it is formatted, the "value" will still be '0.020380774742397016'.

An option is to round the value, then multiply by 100 to allow you to display it as a percentage. Something like:

var valueToReport = (+valueToCheck.toFixed(4))*100;
var message = "Please check Error rates \n\n Reported Maximum Error rate is: " +valueToReport+"%";

This will display the value as "2.04%"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM