我希望执行舍入功能来转换货币。我的基础货币是美元,我在我的商店里有4种其他货币,转换后会显示美分数字。我想收集美分并显示美元。即£46.23变为£46.00 我该怎么办?` 货币转换的代码在这里,但我如何执行舍入功能?
var Currency = {
rates: {"USD":1.0,"EUR":1.18235,"GBP":1.31967,"CAD":0.799475,"ARS":0.056668,"AUD":0.799924,"BRL":0.319752,"CLP":0.0015396,"CNY":0.148679,"CYP":0.397899,"CZK":0.045321,"DKK":0.158978,"EEK":0.0706676,"HKD":0.128031,"HUF":0.00388777,"ISK":0.00968573,"INR":0.0155764,"JMD":0.00784505,"JPY":0.00906149,"LVL":1.57329,"LTL":0.320236,"MTL":0.293496,"MXN":0.05614,"NZD":0.750685,"NOK":0.126912,"PLN":0.277962,"SGD":0.737474,"SKK":21.5517,"SIT":175.439,"ZAR":0.0758898,"KRW":0.000893297,"SEK":0.123782,"CHF":1.03379,"TWD":0.033088,"UYU":0.0353508,"MYR":0.233519,"BSD":1.0,"CRC":0.00174918,"RON":0.259244,"PHP":0.0198146,"AED":0.272236,"VEB":9.89339e-05,"IDR":7.54055e-05,"TRY":0.284221,"THB":0.0300444,"TTD":0.148204,"ILS":0.280674,"SYP":0.00193866,"XCD":0.37037,"COP":0.000334991,"RUB":0.0167319,"HRK":0.159568,"KZT":0.00303551,"TZS":0.000446743,"XPT":938.274,"SAR":0.266651,"NIO":0.0332101,"LAK":0.000121029,"OMR":2.59991,"AMD":0.00208964,"CDF":0.000621197,"KPW":0.00785056,"SPL":6.0,"KES":0.00962681,"ZWD":0.00276319,"KHR":0.000244875,"MVR":0.0649291,"GTQ":0.137316,"BZD":0.499861,"BYR":5.18136e-05,"LYD":0.742597,"DZD":0.00922372,"BIF":0.000576935,"GIP":1.31967,"BOB":0.14458,"XOF":0.00180248,"STD":4.81573e-05,"NGN":0.00317224,"PGK":0.311604,"ERN":0.0652089,"MWK":0.00137597,"CUP":0.0377358,"GMD":0.0217887,"CVE":0.0106932,"BTN":0.0155764,"XAF":0.00180248,"UGX":0.00027751,"MAD":0.105962,"MNT":0.000409398,"LSL":0.0758898,"XAG":16.8011,"TOP":0.463099,"SHP":1.31967,"RSD":0.0098333,"HTG":0.0159471,"MGA":0.000338915,"MZN":0.0163846,"FKP":1.31967,"BWP":0.0986293,"HNL":0.0427342,"PYG":0.000179987,"JEP":1.31967,"EGP":0.0558817,"LBP":0.000663732,"ANG":0.559923,"WST":0.404114,"TVD":0.799924,"GYD":0.00486085,"GGP":1.31967,"NPR":0.00974728,"KMF":0.00240331,"IRR":3.06527e-05,"XPD":884.12,"SRD":0.134302,"TMM":5.77104e-05,"SZL":0.0758898,"MOP":0.124302,"BMD":1.0,"XPF":0.00990809,"ETB":0.0429183,"JOD":1.41168,"MDL":0.0552733,"MRO":0.00278276,"YER":0.00399645,"BAM":0.604526,"AWG":0.558659,"PEN":0.308586,"VEF":0.0989339,"SLL":0.000132348,"KYD":1.21951,"AOA":0.00602612,"TND":0.41731,"TJS":0.113649,"SCR":0.0736818,"LKR":0.00651173,"DJF":0.00561332,"GNF":0.000111298,"VUV":0.00918435,"SDG":0.150282,"IMP":1.31967,"GEL":0.417684,"FJD":0.497719,"DOP":0.0210853,"XDR":1.41115,"MUR":0.0298276,"MMK":0.000735698,"LRD":0.0111047,"BBD":0.5,"ZMK":0.000112534,"XAU":1267.75,"VND":4.40005e-05,"UAH":0.038752,"TMT":0.288552,"IQD":0.000857625,"BGN":0.604054,"KGS":0.0145672,"RWF":0.00119319,"BHD":2.65229,"UZS":0.000244156,"PKR":0.00948842,"MKD":0.0192137,"AFN":0.014605,"NAD":0.0758898,"BDT":0.0122913,"AZN":0.592208,"SOS":0.00172763,"QAR":0.273189,"PAB":1.0,"CUC":1.0,"SVC":0.114286,"SBD":0.128467,"ALL":0.00889658,"BND":0.737474,"KWD":3.31396,"GHS":0.226607,"ZMW":0.112534,"XBT":2866.01,"NTD":0.0337206,"BYN":0.518136},
convert: function(amount, from, to) {
return (amount * this.rates[from]) / this.rates[to];
}
};
`
答案 0 :(得分:1)
使用Math.round()
。
console.log(Math.round(46.23)) //46
答案 1 :(得分:1)
如果要舍入到没有小数的最近数字,可以使用Math.round(num),但是如果要舍入到最接近的数字,可以使用Math.floor(num):
console.log(Math.round(46.23)); // 46
console.log(Math.round(46.73)); // 46
console.log(Math.floor(46.23)); // 46
console.log(Math.floor(46.23)); // 46
如果要添加两个零来获得两位小数,请使用num.toFixed(2)
console.log(Math.round(46.23).toFixed(2)); // 46.00
console.log(Math.round(46.73).toFixed(2)); // 46.00
console.log(Math.floor(46.23).toFixed(2)); // 46.00
console.log(Math.floor(46.23).toFixed(2)); // 46.00