Handling of Foreign Currencies: Difference between revisions
Wolfganguhr (talk | contribs) No edit summary |
Wolfganguhr (talk | contribs) No edit summary |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Actually I've put put the requirements based on [[Foreign Currency Transactions]] and [[Testcase requirements for the CommonAccountingTool]] into a new object | Actually I've put put the requirements based on "[[Foreign Currency Transactions]]" and "[[Testcase requirements for the CommonAccountingTool]]" into a new object TCurrencyInfo (in File GL.CommonTools.cs). The object is tested in Test.GL.CommonTools.cs and the informative test is the routine Test_05_TCurrencyInfo_2. | ||
<pre>TCurrencyInfo tCurrencyInfo = new TCurrencyInfo("EUR", "JPY");</pre> | |||
The constructor of | The constructor of TCurrencyInfo 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. | Then you can use RoundBaseCurrencyValue and - if a pointer is set - RoundForeignCurrencyValue to round a currency value to the correct number of digits. | ||
<pre>Assert.AreEqual(1.23m, tCurrencyInfo.RoundBaseCurrencyValue(1.23456m), "Round to 2 digits"); | |||
Assert.AreEqual(1.0m, tCurrencyInfo.RoundForeignCurrencyValue(1.23456m), "Round to 0 digits");</pre> | |||
Above this you can use ToBaseValue and or ToForeignValue to calculate the foreign currency calculations. | Above this you can use ToBaseValue and or ToForeignValue to calculate the foreign currency calculations. | ||
Line 14: | Line 14: | ||
The example begins with a | The example begins with a | ||
<pre>decimal exchangeRate = 1/119.7295m;</pre> | |||
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. | 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. | ||
<pre>decimal exchangeRate = 1/119.7295m; | |||
Assert.AreEqual(0.84m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate), | |||
"Conversion from 100 YEN to 0.84 EUR"); | |||
Assert.AreEqual(11973, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate), | |||
"Conversion from 100 EUR to 11973 YEN"); | |||
Assert.AreEqual(120, tCurrencyInfo.ToForeignValue(1.00m, exchangeRate), | |||
"Conversion from 1 EUR to 120 YEN"); | |||
tCurrencyInfo.ForeignCurrencyCode = "GBP"; // Change foreign Currency to Pound ... | |||
exchangeRate = 1/0.8801m; | |||
Assert.AreEqual(113.62m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate), | |||
"Conversion from 100 GBP to 113.62 EUR"); | |||
Assert.AreEqual(88.01m, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate), | |||
"Conversion from 100 EUR to 88.01 GBP");</pre> | |||
So you have one object | So you have one object |
Latest revision as of 05:58, 21 April 2011
Actually I've put put the requirements based on "Foreign Currency Transactions" and "Testcase requirements for the CommonAccountingTool" into a new object TCurrencyInfo (in File GL.CommonTools.cs). The object is tested in Test.GL.CommonTools.cs and the informative test is the routine Test_05_TCurrencyInfo_2.
TCurrencyInfo tCurrencyInfo = new TCurrencyInfo("EUR", "JPY");
The constructor of TCurrencyInfo 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, tCurrencyInfo.RoundBaseCurrencyValue(1.23456m), "Round to 2 digits"); Assert.AreEqual(1.0m, tCurrencyInfo.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, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate), "Conversion from 100 YEN to 0.84 EUR"); Assert.AreEqual(11973, tCurrencyInfo.ToForeignValue(100.00m, exchangeRate), "Conversion from 100 EUR to 11973 YEN"); Assert.AreEqual(120, tCurrencyInfo.ToForeignValue(1.00m, exchangeRate), "Conversion from 1 EUR to 120 YEN"); tCurrencyInfo.ForeignCurrencyCode = "GBP"; // Change foreign Currency to Pound ... exchangeRate = 1/0.8801m; Assert.AreEqual(113.62m, tCurrencyInfo.ToBaseValue(100.00m, exchangeRate), "Conversion from 100 GBP to 113.62 EUR"); Assert.AreEqual(88.01m, tCurrencyInfo.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