这是代码,但我如何应用舍入函数??
var Currency = {
rates: {"USD":1.0,"EUR":1.18122,"GBP":1.32077,"CAD":0.796995,"ARS":0.0569,"AUD":0.795582,"BRL":0.319856,"CLP":0.00152967,"CNY":0.148904,"CYP":0.397899,"CZK":0.0451357,"DKK":0.158828,"EEK":0.0706676,"HKD":0.127989,"HUF":0.00389027,"ISK":0.00959953,"INR":0.0156061,"JMD":0.00784898,"JPY":0.00905487,"LVL":1.57329,"LTL":0.320236,"MTL":0.293496,"MXN":0.0558738,"NZD":0.742478,"NOK":0.126237,"PLN":0.277487,"SGD":0.73567,"SKK":21.5517,"SIT":175.439,"ZAR":0.0754504,"KRW":0.000892012,"SEK":0.12331,"CHF":1.03578,"TWD":0.033056,"UYU":0.0353328,"MYR":0.233305,"BSD":1.0,"CRC":0.00174873,"RON":0.2594,"PHP":0.01983,"AED":0.272242,"VEB":0.000100171,"IDR":7.50313e-05,"TRY":0.283307,"THB":0.0300428,"TTD":0.148304,"ILS":0.280826,"SYP":0.00193773,"XCD":0.37037,"COP":0.000336969,"RUB":0.0165942,"HRK":0.159397,"KZT":0.00301383,"TZS":0.000446582,"XPT":943.987,"SAR":0.266649,"NIO":0.0332264,"LAK":0.00012162,"OMR":2.59936,"AMD":0.00209041,"CDF":0.000609222,"KPW":0.00778287,"SPL":6.0,"KES":0.00962554,"ZWD":0.00276319,"KHR":0.000244695,"MVR":0.0646429,"GTQ":0.137389,"BZD":0.499759,"BYR":5.18124e-05,"LYD":0.742485,"DZD":0.00917565,"BIF":0.000575778,"GIP":1.32077,"BOB":0.144825,"XOF":0.00180076,"STD":4.81514e-05,"NGN":0.00272887,"PGK":0.311569,"ERN":0.0652197,"MWK":0.00137704,"CUP":0.0377358,"GMD":0.0217694,"CVE":0.0106952,"BTN":0.0156061,"XAF":0.00180076,"UGX":0.000277656,"MAD":0.105901,"MNT":0.000408806,"LSL":0.0754504,"XAG":16.6766,"TOP":0.466608,"SHP":1.32077,"RSD":0.0098436,"HTG":0.015965,"MGA":0.000338905,"MZN":0.0163874,"FKP":1.32077,"BWP":0.0975059,"HNL":0.042759,"PYG":0.000179926,"JEP":1.32077,"EGP":0.055996,"LBP":0.000663876,"ANG":0.560216,"WST":0.405844,"TVD":0.795582,"GYD":0.00485055,"GGP":1.32077,"NPR":0.00975604,"KMF":0.00240102,"IRR":3.07078e-05,"XPD":893.679,"SRD":0.134352,"TMM":5.78775e-05,"SZL":0.0754504,"MOP":0.124261,"BMD":1.0,"XPF":0.00989865,"ETB":0.0429191,"JOD":1.41174,"MDL":0.0558036,"MRO":0.00278066,"YER":0.00399765,"BAM":0.60395,"AWG":0.558659,"PEN":0.30868,"VEF":0.100171,"SLL":0.000132232,"KYD":1.21951,"AOA":0.00602674,"TND":0.418228,"TJS":0.113568,"SCR":0.0739012,"LKR":0.00652205,"DJF":0.00559876,"GNF":0.000111451,"VUV":0.00947424,"SDG":0.149765,"IMP":1.32077,"GEL":0.418122,"FJD":0.499131,"DOP":0.0211134,"XDR":1.41162,"MUR":0.0299675,"MMK":0.000734012,"LRD":0.0111099,"BBD":0.5,"ZMK":0.000112244,"XAU":1267.94,"VND":4.40025e-05,"UAH":0.0386745,"TMT":0.289387,"IQD":0.000859396,"BGN":0.603619,"KGS":0.0145649,"RWF":0.00119159,"BHD":2.65199,"UZS":0.000243956,"PKR":0.00949031,"MKD":0.0191789,"AFN":0.0146153,"NAD":0.0754504,"BDT":0.0123049,"AZN":0.59192,"SOS":0.00174422,"QAR":0.273026,"PAB":1.0,"CUC":1.0,"SVC":0.114286,"SBD":0.128913,"ALL":0.00889532,"BND":0.73567,"KWD":3.31121,"GHS":0.226584,"ZMW":0.112244,"XBT":2731.17,"NTD":0.0337206,"BYN":0.518124},
convert: function(amount, from, to) {
return (amount * this.rates[from]) / this.rates[to];
}
};

答案 0 :(得分:1)
See this post about .toFixed()
并注意它可能存在准确性问题。
var Currency = {
rates: {"USD":1.0,"EUR":1.18122,"GBP":1.32077,"CAD":0.796995},
convert: function(amount, from, to) {
return ((amount * this.rates[from]) / this.rates[to]).toFixed(2);
}
};
var val; //value to convert
var src; //convert from
var tgt; //convert to
val=12; src="USD"; tgt="GBP";
console.log('$' + val + ' ' + src + ' = $' + Currency.convert(12,src,tgt) + ' ' + tgt);
val=9.45; src="USD"; tgt="CAD";
console.log('$' + val + ' ' + src + ' = $' + Currency.convert(12,src,tgt) + ' ' + tgt);