如何去掉nodejs/html中的科学计数法,只显示十进制?

How to remove scientific notation in nodejs/html and display in decimal only?

我正在使用节点 JS 应用程序通过 ses.sendTemplatedEmail API

使用亚马逊 SES 发送电子邮件

我还提到了模板的纯 html 文件供需要时参考

如何实现预期输出?我不知道出了什么问题以及为什么在 JSON

中指定小数时使用科学

test.js

const aws=require('aws-sdk')
const ses = new aws.SES({ apiVersion: "2020-12-01",region:"us-east-1"});
var a = {
    "Items": [
        {
            "timeSinceInactive": 0.00011574074074074075,
            "id": "myid1",
            "ssid": "myssid",
            "deviceName": "devicename1"
        },
        {
            "timeSinceInactive": 0.00009259259259259259,
            "id": "myid2",
            "ssid": "myssid2",
            "deviceName": "devicename2"
        }
    ]
}
b = JSON.stringify(a)
console.log(b)
async function sesSendEmail(b,ses) {
    var params = {
      Source: "abc@gmail.com",
      Template: "mytemplatename",
      Destination: {
        ToAddresses: ["xyz@gmail.com"] // Email address/addresses that you want to send your email
      },
      TemplateData: b,
    }
    try {
      await ses.sendTemplatedEmail(params).promise()
    }
    catch (err) {
      console.log(err)
      throw new Error(err)
    }
  };
  function setAwsCredentials() {
    awsCredentials = {
      region: "us-east-1",
      accessKeyId: "",
      secretAccessKey: ""
    };
    aws.config.update(awsCredentials);
    console.log("Set credentials successfully")
  }
setAwsCredentials()
sesSendEmail(b,ses)

template.html

<table border='2' width='1000'>
    <tr>
        <th>timeSinceInactive</th>
        <th>id</th>
        <th>ssid</th>
        <th>deviceName</th>
    </tr>
    <thead>
        <tr>
            {{#each Items.[0]}}
            {{/each}}
        </tr>
    </thead>
    <tbody>
        {{#each Items}}
        <tr>
            {{#each this}}
            <td>
                 {{this}}
            </td>
            {{/each}}
        </tr>
        {{/each}}
    </tbody>
</table>

当前输出

timeSinceInactive        id      ssid    deviceName
1.1574074074074075E-4   myid1   myssid  devicename1
9.259259259259259E-5    myid2   myssid2 devicename2

期望输出

timeSinceInactive        id      ssid    deviceName
0.00011574074074074075  myid1   myssid  devicename1
0.00009259259259259259  myid2   myssid2 devicename2

编辑

我还需要对数据进行排序,因此不幸的是,将其转换为字符串并不是一个可行的选择。

备注

我正在使用支持车把的 Amazon SES 中的 createTemlate API。因此 html 代码默认使用车把...这导致问题以科学记数法表示出于某种原因,我如何将其设为十进制?

您可以考虑先将数字转换为字符串,然后再将它们传递给模板。这样您就可以控制格式。

在我看来,您应该切换到一个不太通用的模板,并调用一个可以根据需要显示号码的自定义助手。模板将如下所示:

<table border='2' width='1000'>
    <tr>
        <th>timeSinceInactive</th>
        <th>id</th>
        <th>ssid</th>
        <th>deviceName</th>
    </tr>
    <thead>
        <tr>
            {{#each Items.[0]}}
            {{/each}}
        </tr>
    </thead>
    <tbody>
        {{#each Items}}
        <tr>
            <td>
               {{customFormatNumber timeSinceInactive}}
            </td>
            <td>
               {{id}}
            </td>
            <td>
               {{ssid}}
            </td>
            <td>
               {{deviceName}}
            </td>
        </tr>
        {{/each}}
    </tbody>
</table>

这是您可以编写的自定义助手(这只是一个示例,可能不是您想要的):

Handlebars.registerHelper('customFormatNumber ', function(item, options) {
    retrun item.toPrecision(10)
});