简体   繁体   English

添加用户输入而不四舍五入(Google Apps脚本)

[英]Adding User input without rounding (Google Apps Script)

I am adding a users input in to UI as they add numbers and returning the results. 我在用户添加数字并返回结果时将用户输入添加到UI中。 The input is currency so I need to carry it out two decimals and not round. 输入是货币,因此我需要将其保留两位小数而不是四舍五入。

Here is an example of my code: 这是我的代码示例:

    function ceiling2(number) {
  var ceiling2;
  return ceiling2 = Math.ceil(number*100)/100;
}
//Totals
function lD23Total (e){
  var app = UiApp.getActiveApplication();
  var tB1v = parseInt(e.parameter.TextBox1);
  var tB9v = parseInt(e.parameter.TextBox9);
  var tB17v = parseInt(e.parameter.TextBox17);
  var tB25v = parseInt(e.parameter.TextBox25);
  var tB33v = parseInt(e.parameter.TextBox33);
  var tB41v = parseInt(e.parameter.TextBox41);
  var tB49v = parseInt(e.parameter.TextBox49);
  var lD23 = app.getElementById("LabelD23").setStyleAttribute('fontWeight','bold');
  var lD23T = tB1v + tB9v + tB17v + tB25v + tB33v + tB41v + tB49v;
  lD23.setText("$ " + ceiling2(lD23T));

  var app = UiApp.getActiveApplication();
  app.close();
  return app;  
}

Currently it returns a rounded number. 当前,它返回一个四舍五入的数字。

I appreciate an suggestions you may offer! 感谢您提出的建议!

Jon 乔恩

The function parseInt() will convert the value to an integer, dropping the values after the decimal. 函数parseInt()会将值转换为整数,将值删除到小数点后。 Use parseFloat() instead. 请改用parseFloat()。

I think your function is just fine... if you remove the parseInt() and replace it either with parseFloat()as suggested by Eric or by Number() it should work... if not then the problem might come from the way numbers are written: 我认为您的功能很好...如果您删除parseInt()并按照Eric或Number()的建议将其替换为parseFloat(),它应该可以正常工作...否则,问题可能出在数字写为:

If you used 26,4567 but you should use 26.4567 with a dot as separator in place of a comma. 如果您使用的是26,4567,但应使用26.4567(带点作为分隔符)代替逗号。

Could you try and keep us informed ? 您可以尝试使我们了解情况吗? regards, Serge 问候,Serge

Or you can use this before sending to your function: 或者,您可以在发送给函数之前使用它:

var newnumber=Number(number.toString().replace(",","."));// convert to string, replace comma with dot and set as number again.

and your function will work in both cases 并且您的功能在两种情况下都可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google Apps脚本上提示用户输入,使用getUi()方法出错 - Prompt user input on Google Apps Script, error with `getUi()` method 使用 Google Apps 脚本共享云端硬盘文档而不通知用户 - Share a Drive document without notifying user with Google Apps Script 在 Google Apps 脚本 web 应用程序中动态添加 Materialize select 输入时出错 - Error dynamically adding Materialize select input in Google Apps Script web app 谷歌应用程序脚本添加一个组以草拟电子邮件 - google apps script adding a group to draft email 将“密码”类型添加到Google Apps脚本inputBox - Adding “password” type to Google Apps Script inputBox 在 Google Apps 脚本中添加 Bootstrap 4 Searchable Dropdown - Adding Bootstrap 4 Searchable Dropdown in Google Apps Script 将 JSON 添加到 Google Apps 脚本中的属性 - Adding JSON to properties in Google Apps Script Google Apps脚本:如何在不使用getComment()方法的情况下在第二条注释中添加第二条注释 - Google Apps Script: how to adding a second comment to the first without using getComment() method 范围输入以在Google Apps脚本中起作用 - Range input to function in Google apps script 使用 Google Apps 脚本变量预填充 HTML 输入 - Prefill an HTML input with Google Apps Script variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM