Handling of Foreign Currencies: Difference between revisions
Wolfganguhr (talk | contribs) No edit summary |
Wolfganguhr (talk | contribs) No edit summary |
||
Line 23: | Line 23: | ||
Assert.AreEqual(11973, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 11983 YEN"); | Assert.AreEqual(11973, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 11983 YEN"); | ||
Assert.AreEqual(120, getCurrencyInfo.ToForeignValue(1.00m, exchangeRate), "Conversion from 1 EUR to 120 YEN"); | Assert.AreEqual(120, getCurrencyInfo.ToForeignValue(1.00m, exchangeRate), "Conversion from 1 EUR to 120 YEN"); | ||
// | |||
getCurrencyInfo.ForeignCurrencyCode = "GBP"; // Change foreign Currency to Pound ... | getCurrencyInfo.ForeignCurrencyCode = "GBP"; // Change foreign Currency to Pound ... | ||
exchangeRate = 1/0.8801m; | exchangeRate = 1/0.8801m; | ||
// | |||
Assert.AreEqual(113.62m, getCurrencyInfo.ToBaseValue(100.00m, exchangeRate), "Conversion from 100 GBP to 113.62 EUR"); | Assert.AreEqual(113.62m, getCurrencyInfo.ToBaseValue(100.00m, exchangeRate), "Conversion from 100 GBP to 113.62 EUR"); | ||
Assert.AreEqual(88.01m, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 88.01 GBP"); | Assert.AreEqual(88.01m, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 88.01 GBP"); |
Revision as of 08:12, 5 April 2011
Actually I've put put the requirements based on Foreign Currency Transactions and Testcase requirements for the CommonAccountingTool into a new object GetCurrencyInfo (in File GL.CommonTools.cs). The object is tested in Test.GL.CommonTools.cs and the informative test is the routine Test_05_GetCurrencyInfo_2.
GetCurrencyInfo getCurrencyInfo = new GetCurrencyInfo("EUR", "JPY");
The constructor of GetCurrencyInfo contains a loadAll-database request for the table a_currency. In this table you can set two row pointer one for the base currency and one for the foreign currency. The base pointer is set once by the constructor and cannot be changed later.
Then you can use RoundBaseCurrencyValue and - if a pointer is set - RoundForeignCurrencyValue to round a currency value to the correct number of digits.
Assert.AreEqual(1.23m, getCurrencyInfo.RoundBaseCurrencyValue(1.23456m), "Round to 2 digits"); Assert.AreEqual(1.0m, getCurrencyInfo.RoundForeignCurrencyValue(1.23456m), "Round to 0 digits");
Above this you can use ToBaseValue and or ToForeignValue to calculate the foreign currency calculations.
The example begins with a
decimal exchangeRate = 1/119.7295m;
where 119.7295 is the todays value elsewhere from the internet and in Testcase requirements for the CommonAccountingTool I've explained why we have to use the reziprokal value here.
decimal exchangeRate = 1/119.7295m; Assert.AreEqual(0.84m, getCurrencyInfo.ToBaseValue(100.00m, exchangeRate), "Conversion from 100 YEN to 0.83 EUR"); Assert.AreEqual(11973, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 11983 YEN"); Assert.AreEqual(120, getCurrencyInfo.ToForeignValue(1.00m, exchangeRate), "Conversion from 1 EUR to 120 YEN"); // getCurrencyInfo.ForeignCurrencyCode = "GBP"; // Change foreign Currency to Pound ... exchangeRate = 1/0.8801m; // Assert.AreEqual(113.62m, getCurrencyInfo.ToBaseValue(100.00m, exchangeRate), "Conversion from 100 GBP to 113.62 EUR"); Assert.AreEqual(88.01m, getCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 88.01 GBP");
So you have one object
- to handle all available currency infos
- and of course the base currency infos too
- can easily be switched to an other currency
- handles the base-foreign calculation correctly in the sense of om