【问题标题】:how do i convert dropdown list all values into Million/Billion in javascript?如何在javascript中将下拉列表中的所有值转换为百万/十亿?
【发布时间】:2020-09-14 08:54:57
【问题描述】:

如何转换由这些示例中的代码生成的下拉列表中的所有输出数字? (例如:如果输出数字是 100 0000 我想将其显示为 1M(百万) Ex2:如果输出数字是 1000 000 000 我想将其显示为 1B(billion) )

window.f = function(event) {
    var ddlArray = new Array();
    var onoffelem=event.target;
    var ddl = document.getElementById('ddl');
    var select = document.getElementById('combo2');
    var combo3 = document.getElementById('combo3');
    var selectedVal=onoffelem.options[onoffelem.selectedIndex].text ;

    if (selectedVal == "LKR") {
        document.getElementById("combo2").options.length = 0;
        for (i = 0; i < combo3.options.length; i++) {
            ddlArray[i] = combo3.options[i].value;
        }
        for (i = 0; i < ddlArray.length; i++) {
            var option = document.createElement("OPTION"),
            txt = document.createTextNode("Rs. " + ddlArray[i] * 185000000);
            option.appendChild(txt);
            option.setAttribute("value", ddlArray[i] * 185000000);
            select.insertBefore(option, select.lastChild);
        }
    } 

    if (selectedVal == "USD") {
        document.getElementById("combo2").options.length = 0;
        for (i = 0; i < combo3.options.length; i++) {
            ddlArray[i] = combo3.options[i].value;
        }
        for (i = 0; i < ddlArray.length; i++) {
            var option = document.createElement("OPTION"),
            txt = document.createTextNode(ddlArray[i] * 20000);
            option.appendChild(txt);
            option.setAttribute("value",ddlArray[i] * 20000);
            select.insertBefore(option,select.lastChild);
        }
    }
}

 
<select id="ddl" onchange="f(event)" >
    <option value="185">USD</option>
    <option value="11">LKR</option>
    <option value="48">AED</option>
</select>
<select name="" id="combo2" >
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<select name="" id="combo3" hidden="">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

【问题讨论】:

  • K/M/B 是什么意思?
  • 我想把长数转换成百万/十亿/千...
  • 您能否提供一个示例输入以及您希望从输入中得到什么样的输出?
  • 当我从第一个下拉列表中选择货币时..我想在第二个下拉列表中显示货币对话..&我希望转换后的货币显示为十亿/百万/千...因为我的输出数字太长,这就是为什么我想在 javascript 中将长数字转换为缩写字符串,并具有特殊的简短要求..
  • edit您的问题,而不是添加 cmets。最初可能会或可能不会显示评论,并且通常按投票排序,而不是发布时间。通过将所有信息放在一个地方,即问题,可以轻松帮助您。还请说明:您当前的代码有什么问题?当你运行它时会发生什么?你期望会发生什么?浏览器控制台有什么错误吗?

标签: javascript html arrays loops for-loop


【解决方案1】:

您可以使用类似于此的功能:

formatNumber(num) {
    if (num >= 1000000000) {
      return (num / 1000000000).toFixed().replace(/\.0$/, '') + 'B';
    }
    if (num >= 1000000) {
      return (num / 1000000).toFixed().replace(/\.0$/, '') + 'M';
    }
    if (num >= 1000) {
      return (num / 1000).toFixed().replace(/\.0$/, '') + 'K';
    }
}

【讨论】:

  • 您应该在设置选项标签时调用此函数。像这样:txt = document.createTextNode("Rs." + formatNumber(ddlArray[i] * 185000000));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多