Javascript Google 电子表格中的圆形变量

Dav*_*nde 0 javascript variables rounding

我正在查询一个谷歌电子表格,我得到的数字正在增加我在代码中进一步做的一些权重。

我的查询:

function queryValue3(met1,met2,met3) {
var query3 = new google.visualization.Query('https://spreadsheets.google.com/spreadsheet/tq?range=B7:B9&key=my_key&gid=10');
query3.send(function (response) {
    if (response.isError()) {
        alert('Error in query3: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
    }

    var data3 = response.getDataTable();

    var m1 = data3.getValue(0, 0);
    var m2 = data3.getValue(1, 0);
    var m3 = data3.getValue(2, 0);
Run Code Online (Sandbox Code Playgroud)

我为 m2 提取的数字显然是 25.3333333333333323,稍后在函数中让我大吃一惊。只要我的数字四舍五入到第 10 个或第 100 个(在这种情况下我想要第 10 个),该函数就可以正常工作。我使用自定义舍入(1 位小数),它在工作表中看起来是正确的,但查询正在获取原始数字?在大多数情况下,我的数字是整数,并且在这些情况下我希望它没有小数。有人可以帮我修改这种情况下的变量 m1、m2 和 m3 吗?

谢谢...

Spe*_*art 6

n.toFixed(2);

您可以使用它来设置小数点后的数字数量。

例子:

foo = 25.3333333333;
foo = +foo.toFixed(2); 
// The plus makes it a number-type again, as toFixed converts to string.
console.log(foo); // 25.33
Run Code Online (Sandbox Code Playgroud)

编辑:十进制舍入作为预期的副作用发生:

foo = 25.127;
foo = +foo.toFixed(2); 
console.log(foo); // 25.13
Run Code Online (Sandbox Code Playgroud)

整数保持不变:

foo = 23;
foo = +foo.toFixed(2);
console.log(foo); // 23
Run Code Online (Sandbox Code Playgroud)