在网站上看不到太多小数+样式

时间:2013-04-25 12:44:52

标签: javascript colors styling decimal

我的脚本在返回值

中显示太多小数点

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

你可以告诉我我失踪了吗?

感谢

<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>

2 个答案:

答案 0 :(得分:1)

仅适用于JavaScript:

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

.toFixed()

就可以了。

例如:

var i = 3.4444

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

在此处了解详情:Mozilla Developer NetworkECMAScript Specification

答案 1 :(得分:1)

关于Javascript / Decimals

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

以下是如何将其合并到当前结构中(您必须对每个case语句执行类似的操作):

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;
}