Search code examples
htmlnode.jsamazon-web-serviceshandlebars.jsamazon-ses

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


I am using a node JS app to send an email using amazon SES using the ses.sendTemplatedEmail API

I have also mentioned the template's pure html file for reference if need be

how do i achieve expected output ? i don't know what is going wrong and why it is using scientific when decimal is specified in 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>

Current output:

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

desired output

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

EDIT

I need to sort the data as well so converting it to a string is not a feasible alternative unfortunately.

Note

I am using createTemlate API from Amazon SES which supports handlebars.. so the html code is using handlebars by default... that is causing the issue its making it in scientific notation for some reason how do i make it decimal only ?


Solution

  • You might consider converting the numbers to strings before passing them to the template. That way you have control over the formatting.