0

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.

Diego
  • 9,261
  • 2
  • 19
  • 33
Daniel Bertoldi
  • 359
  • 4
  • 14
  • FYI, using `Count()` in the condition part of your `for` loop causes it to go through the list completely on every iteration. Either lose the `.ToList()` and use `lines.Length` (since you're not using the features of `List`), or keep the `.ToList()` and use `.Skip(1)` followed by a `foreach` loop. You're also resetting `price` on every iteration; not sure why you'd do that. – Heretic Monkey May 13 '19 at 21:05
  • @HereticMonkey sorry, I forgot to clarify why I'm "resetting" the ```price```. For simplicity reasons I cut part of the real code, I didn't realized it would make my problem more harder to understand, and for that I'm sorry. The variable ```price``` it's an attribute of a class called ```Coin```. I create a list of type ```Coin``` and then I start to add new ```Coins``` to the list with the values of every line. I'll update my problem description. – Daniel Bertoldi May 13 '19 at 21:24
  • @HereticMonkey Oh, and thank you for the tips! – Daniel Bertoldi May 13 '19 at 21:30

0 Answers0