1

我下面的脚本在返回值中显示了太多的小数点

另外,当我将代码发布到我的网站上时,它没有显示我添加的所有文本样式

你能告诉我我错过了什么吗?

谢谢

<html>

    <head>
    <style>
            body {color:rgb(128,128,128);}
            p.3 {color:rgb(128,128,128);}
            p.3 {font-size:20PX;}
    </style>

    <script type = "text/javascript">

    function convert() {
        var Amount=document.getElementById('amount');
        var Currency=document.getElementById('currency');
        var Converted=document.getElementById('converted');
        var Choice=document.getElementById('choice');
        var AED=1;
        var US=0.27;
        var QR=0.99;
        var SR=1.02;
        var KD=0.0778;
        var BD=0.102;

        switch(document.converter.currency.value) {
            case "US Dollars" :
                document.converter.converted.value=US*document.converter.amount.value;
                document.converter.choice.value=document.converter.currency.value;
            break;
            case "Qatar Riyal":
                document.converter.converted.value=QR*document.converter.amount.value;
                document.converter.choice.value=document.converter.currency.value;
            break;
            case "Saudi Riyal":
                document.converter.converted.value=SR*document.converter.amount.value;
                document.converter.choice.value=document.converter.currency.value;
            break;
            case "Kuwaiti Dinar":
                document.converter.converted.value=KD*document.converter.amount.value;
                document.converter.choice.value=document.converter.currency.value;
            break;
            case "Bahrain Dinar":
                document.converter.converted.value=BD*document.converter.amount.value;
                document.converter.choice.value=document.converter.currency.value;
            break;
        }
    }
    </script>

    </head>

    <body>
        <div id="divWrapper">
        <form name="converter" id="converter">
        <br/> Enter amount in UAE Dirhams
        <input name="amount"type="text" id="amount" size="3" />
        <br /><br /> 
        Please Select a currency <select name="currency" id="currency">
        <option>Select one</option>
        <option value="US Dollars">US Dollars</option>
        <option value="Qatar Riyal">Qatar Riyal</option>
        <option value="Saudi Riyal">Saudi Riyal</option>
        <option value="Kuwaiti Dinar">Kuwaiti Dinar</option>
        <option value="Bahrain Dinar">Bahrain Dinar</option>
        </select> 

        <input type="button" name="convt" id="convt" onclick="convert()" value="Convert" />
        <br /><br /> 
        <p class="3"> The amount is:
        <input name="converted" type="text" id="converted" style="border:0px" style="color:rgb(255,0,102)" style="text-align:center" style="font-size:22PX"  value="" size="5"/>
        in 
        <input name="choice" type="text" id="choice" style="border:0px" style="color:rgb(255,0,102)" style="font-size:22PX" style="text-align:center" value="" size="10"> </p>

        <br /><br />
        </form>
        </div>
    </body>

</html>
4

2 回答 2

1

仅适用于 JavaScript:

如果要将数字格式化为固定的小数位数,请致电

.toFixed()

在上面。

例如:

var i = 3.4444

var num = i.toFixed(2) //num = 3.44, but as a string

在这里查看更多信息:Mozilla 开发者网络ECMAScript 规范

于 2013-04-25T13:31:46.100 回答
1

关于 Javascript/小数

假设您希望将事物四舍五入到小数点后两位,您应该使用该.toFixed()函数,如tschulze所述。

以下是如何将其合并到您当前的结构中(您必须对每个案例陈述做类似的事情):

document.converter.converted.value=(BD*document.converter.amount.value).toFixed(2);
document.converter.choice.value=document.converter.currency.value;

关于造型

您在样式方面遇到的问题是由于具有多个样式属性。应用多种样式的正确方法是将您设置的所有不同方面设置在一个样式属性中,用分号分隔:

<input name="converted" type="text" id="converted" style="border:0; color:rgb(255,0,102); text-align:center; font-size:22PX;" value="" size="5"/>

补充笔记

似乎您可以通过切换到具有单个集合乘数的结构来真正简化代码,然后根据适合的情况选择要应用的结构:

function convert() {
    var Amount=document.getElementById('amount');
    var Currency=document.getElementById('currency');
    var Converted=document.getElementById('converted');
    var Choice=document.getElementById('choice');
    var multiplier=1;

    switch(document.converter.currency.value) {
        case "US Dollars" : multiplier=0.27; break;
        case "Qatar Riyal": multiplier=0.99; break;
        case "Saudi Riyal": multiplier=1.02; break;
        case "Kuwaiti Dinar": multiplier=0.0778; break;
        case "Bahrain Dinar": multiplier=0.102; break;
    }
    
     document.converter.converted.value=(multiplier*document.converter.amount.value).toFixed(2);
     document.converter.choice.value=document.converter.currency.value;
}
于 2013-04-25T13:39:53.553 回答