dongmibeng5885 2018-06-08 16:46
浏览 60

Javascript或Jquery / PHP / Web Socket编号格式问题

I'm modifying one script and learning JS and Web Sockets while changing it to my needs.

Script is a simple calculator between BTC and USD.

Code works fine, but I cant get proper number formatting since I'm not certain should I do it in JS or PHP.

HTML

<input type="text" class="form-control" id="amtUSD" value="<?php echo $data['price_usd']; ?>">

JS

var ember = [];
var usdInput = document.getElementById("amtUSD");
var btcInput = document.getElementById("amtBTC");

ember.btcPrice = <?php echo $data['price_usd']; ?>;
ember.socket = io.connect('https://socket.coincap.io');
ember.socket.on('trades', function(data) {
  var amt = document.getElementById("amtUSD");
    if (data.coin == "BTC") {
    ember.amtUSD = usdInput.value;
    ember.amtBTC = btcInput.value;
    ember.usdCalc = ember.amtBTC * data.msg.price;
    if(data.msg.price > amt.value) {
      $(amt).addClass('increment');
    } else {
      $(amt).addClass('decrement');
    }
    $("#amtUSD").attr("value", ember.usdCalc);
    setTimeout(function () {
        $(amt).removeClass('increment decrement');
    }, 700);
}
});

$("#amtBTC").bind("change paste keyup", function() {
ember.usdCalc = $(this).val() * ember.btcPrice;
$("#amtUSD").attr("value", ember.usdCalc);
});

I tried with PHP. It works for the first value, but as soon as it gets new value, it reverts to non formatted value.

<?php echo number_format($data['price_usd'], 2); ?>

So, I presume I need to format numbers in JS or Jquery - it doesnt matter for this case, whichever is simpler.

Thanks in advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名