开发者

COnvert £'s to $'s

开发者 https://www.devze.com 2023-02-20 05:44 出处:网络
Hi may seem a bit of a dumb question but does anybodyknow some simple Java cod开发者_如何学Goe to convert uk £\'s to us $\'s? Thanks!I think you need to consume a web-service which can Provide real-t

Hi may seem a bit of a dumb question but does anybodyknow some simple Java cod开发者_如何学Goe to convert uk £'s to us $'s? Thanks!


I think you need to consume a web-service which can Provide real-time currency foreign exchange information and calculations.

example http://www.xignite.com/xCurrencies.asmx?WSDL


String currency = "£100";

currency = currency.replace( '£', '$' );

:)


If you've got the £/$ rate, just do 100$ * £/$ rate = x £. Note that for financial calculations it is generally advisable to use BigDecimal, although in that simple case the precision errors should not be that big a problem.


Just a simple calculation will do:

1 U.S. dollar = 0.6201935 British pounds (Taken from Google)

var exchangerate = 0.6201935;
var dollars = 10;
var pounds = dollars * exchangerate ;

or

var exchangerate = 0.6201935;
var pounds = 10;
var dollars = pounds / exchangerate ;


Actually that rate of exchange may vary day to day so there are some websrvcies which will provide you day to day change of exchange rate. Please adopt those.


The change rate for GBPUSD is usually quoted as how many dollars you get for one sterling. This could be 1.61162.

double rate = 1.61162;
double dollars = 10;
double pounds = round(dollars / rate, 2); // round to two decimal places

or

double pounds = 10;
double dollars = round(pounds * rate, 2);

You can use BigDecimal, but the process is the same.

Exchange rates can change many times per second. Its also worth noting that you get a different rate depending on whether you are buying or selling dollars. i.e. if you buy dollars and then sell them you usually won't get as much money back.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号