I'm trying to display in a web page the actual prices of different currencies using ASP .NET and C#. The problem is that if I open my browser with the sheet that my algorithm uses to read values from and the web page that should display it, sometimes the values won't match.
For example, let's say that we have a value of ¥0.03810 in the Google Sheet. When I open the web page, it may show the exact value, ¥0.03810 or it may actually show really different values, like ¥0.03815 or even ¥0.03803.
I've already tried a lot of things. First, I thought it would be because the algorithm was rounding up numbers as it pleased since there are no criteria whatsoever as to how it should print the number. Turns out that wasn't the case since I've already tried:
- Converting the string read from the sheet to a decimal/double and then converting it again to a string.
- Actually keeping the value as decimal/double and then doing some simple mathematical manipulations on it like multiplying by 100 and then dividing by 100. Still no progress.
- Casting the value to a string before assigning to a variable.
Anyways, to read each and every single line from my Google Sheet I use a WebClient
that downloads a .tsv
version of it, then I split the string and turn it into a list.
List<Coin> coins = new List<Coin>();
string priceList = wc.DownloadString(xls);
var lines = priceList.Split('\n').ToList();
Now that I have all of my lines as a list, I use a for loop
to iterate through them and get the currency price of each one of them.
for (int x = 1; x < lines.Count(); x++) {
var item = lines[x].Replace("\r", "").Split('\t');
coins.Add(new Coin() {
price = item[4],
//assigning other attributes...
}
}
The item[4]
contains the currency price in the Google Sheet.
The Google Finance formula I'm using is:
=GoogleFinance("CURRENCY:USDBRL";"average")
When I finally print the price
in the web page, most of the time (it seems to become even more unstable the more decimal places there is) it'll show other values than the one that my sheet actually shows.
Now I'm not really sure if the algorithm actually rounds up some numbers when it fetches the values inside the Google Sheet or if the Google Finance formula actually changes its value every few seconds, and the value shown on the web page is the right one since it's always downloading the latest version of the sheet and running the Google Finance formula together.