alfie
alfie

Reputation: 11

COnvert £'s to $'s

Hi may seem a bit of a dumb question but does anybodyknow some simple Java code to convert uk £'s to us $'s? Thanks!

Upvotes: 1

Views: 496

Answers (6)

Peter Lawrey
Peter Lawrey

Reputation: 533790

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.

Upvotes: 0

Nick
Nick

Reputation: 25808

String currency = "£100";

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

:)

Upvotes: 5

developer
developer

Reputation: 9478

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.

Upvotes: 0

Lalchand
Lalchand

Reputation: 7827

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

Upvotes: 5

CeejeeB
CeejeeB

Reputation: 3114

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 ;

Upvotes: 0

Thomas
Thomas

Reputation: 88747

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.

Upvotes: 0

Related Questions