[英]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.