JS自动添加逗号

时间:2018-02-27 18:45:50

标签: javascript

当我从API获取信息时,我希望我的输出自动包含逗号const express = require('express'); const bodyParser = require('body-parser'); const nodemailer = require('nodemailer'); const app = express(); var port = process.env.PORT || 8800; app.use(bodyParser.json()); app.post('/', function(req, res) { var messageBody = {}; messageBody.name = req.body.name; messageBody.amount = req.body.amount_in_cash; messageBody.phone = req.body.phone; messageBody.email = req.body.email; if (!req.body.uid) { return console.log("No UID"); } var transporter = nodemailer.createTransport({ service : 'smtp.gmail.com', auth : { user : 'example@gmail.com', pass : 'xxxxxx' } }); var mailOptions = { from: 'example@gmail.com', to : 'example@email.com', subject : "New Donation Lead", text : `Name: ${messageBody.name}\nPhone: ${messageBody.phone}\nEmail: ${messageBody.email}\nAmount: ${messageBody.amount}` } transporter.sendMail(mailOptions, function(error, info) { if(error) { console.log(error); } else { console.log("message sent"); } }); res.send(200).end(); }); app.listen(port, () => console.log('Listening on port: '+ port)); 。通常我会使用($279127832954 -> $279,127,832,954),但这样做不起作用。

我的javsscript代码:

.toLocaleString()

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你必须做.toFixed()而不是toFixed,因为它是一种方法而不是财产。然后.toLocaleString()似乎工作正常。

请参阅以下代码:

$.get("https://api.coinmarketcap.com/v1/global/", function(data) {
  var val = (data.total_market_cap_usd / 100).toFixed() * (100 - data.bitcoin_percentage_of_market_cap).toFixed();
  val = val.toLocaleString();
  $("#totalalt_market_cap_usd").html("$" + val);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="totalalt_market_cap_usd"></div>