none
Function to calculate from json RRS feed

  • Question

  • Hi. 

    I have a Json at the bottom of the post. I need a function to get always the value in Euro based in a input currency parameter. For example:

    private decimal ConvertirMontoAEuro(decimal monto, string currency)
            {
                if currency is USD, monto has to be converted to CAD and then convert to EUR

                if currency is AUD, monto has to converted to USD and then convert to CAD and then convert to EUR

                return monto * rate;
            }

    How could I do this function?.

    Thanks in advance.

    [  
       {  
          "from":"CAD",
          "to":"USD",
          "rate":"1.26"
       },
       {  
          "from":"USD",
          "to":"CAD",
          "rate":"0.79"
       },
       {  
          "from":"CAD",
          "to":"EUR",
          "rate":"1.07"
       },
       {  
          "from":"EUR",
          "to":"CAD",
          "rate":"0.93"
       },
       {  
          "from":"USD",
          "to":"AUD",
          "rate":"1.27"
       },
       {  
          "from":"AUD",
          "to":"USD",
          "rate":"0.79"
       }
    ]


    Thursday, July 12, 2018 7:31 PM

Answers

  • Do you already know how to read the JSON into a C# structure?  There are lots of examples on the web of how to do that.

    The conversion problem is more interesting.  What I would do is build a complete to/from table as a two-dimensional array (or, more likely, Dictionary<String,Dictionary<String,decimal> >, so that lookup["USD"]["CAD"] gives the conversion rate).

    Now, let's say you scan through the JSON.  For the first entry, you can fill in lookup["CAD"]["USD"].  For the second entry, you can fill in lookup["USD"]["CAD"], and that's it.

    But for the third entry, after you fill in ["CAD"]["EUR"], you now have the ability to fill in the USD->EUR conversion.  You know USD->CAD, and you now know CAD->EUR, so you just multiply the two.  After you read in the last entry, you will have all 16 conversions filled out.  After that, doing the conversion itself is easy.

    using System;
    using System.IO;
    using System.Text;
    using System.Collections.Generic;
    
    public class Program
    {
        static Dictionary<string, Dictionary<string, decimal> > table;
    
        static void Main(string[] args)
        {
            string[] inputData = new string[]{
                "CAD USD 1.26",
                "USD CAD 0.79",
                "CAD EUR 1.07",
                "EUR CAD 0.93",
                "USD AUD 1.27",
                "AUD USD 0.79"};
    
            List<string> names = new List<string>();
    
            // First, look up all the names.
    
            foreach( var line in inputData )
            {
                string[] parts = line.Split(' ');
                if( !names.Contains(parts[0]) )
                    names.Add( parts[0] );
                if( !names.Contains(parts[1]) )
                    names.Add( parts[1] );
            }
    
            // Now make the array.
    
            table = new Dictionary<string, Dictionary<string, decimal>>();
            foreach( var n1 in names )
            {
                table[n1] = new Dictionary<string, decimal>();
                foreach( var n2 in names )
                {
                    table[n1][n2] = 0;
                }
                table[n1][n1] = 1;
            }
    
            // Now fill in the array.
    
            foreach( var line in inputData )
            {
                string[] parts = line.Split(' ' );
                string from = parts[0];
                string to = parts[1];
                decimal rate = Decimal.Parse( parts[2] );
    
                // Fill in the main conversion.
    
                table[from][to] = rate;
    
                // See if we can fill in any other conversions.
    
                foreach( var name in names )
                {
                    if( name == from || name == to )
                        continue;
                    // If I know CAD->EUR, and I already knew USD->CAD, then
                    // I can fill in USD->EUR.
    
                    if( table[name][to] == 0 && table[name][from] != 0 )
                        table[name][to] = table[name][from] * rate;
    
                    // If I know CAD->EUR and I already knew EUR->USD, then
                    // I can now fill in CAD->USD.
    
                    if( table[from][name] == 0 && table[to][name] != 0 )
                        table[from][name] = table[to][name] * rate;
                }
            }
    
            // Now I know all the rates.
    
            foreach( var row in table )
            {
                foreach( var col in row.Value )
                {
                    Console.WriteLine( "{0}->{1} = {2}",
                        row.Key, col.Key, col.Value
                    );
                }
            }
        }
    }
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by diegodamage82 Friday, July 13, 2018 2:31 AM
    Friday, July 13, 2018 12:37 AM

All replies

  • Do you already know how to read the JSON into a C# structure?  There are lots of examples on the web of how to do that.

    The conversion problem is more interesting.  What I would do is build a complete to/from table as a two-dimensional array (or, more likely, Dictionary<String,Dictionary<String,decimal> >, so that lookup["USD"]["CAD"] gives the conversion rate).

    Now, let's say you scan through the JSON.  For the first entry, you can fill in lookup["CAD"]["USD"].  For the second entry, you can fill in lookup["USD"]["CAD"], and that's it.

    But for the third entry, after you fill in ["CAD"]["EUR"], you now have the ability to fill in the USD->EUR conversion.  You know USD->CAD, and you now know CAD->EUR, so you just multiply the two.  After you read in the last entry, you will have all 16 conversions filled out.  After that, doing the conversion itself is easy.

    using System;
    using System.IO;
    using System.Text;
    using System.Collections.Generic;
    
    public class Program
    {
        static Dictionary<string, Dictionary<string, decimal> > table;
    
        static void Main(string[] args)
        {
            string[] inputData = new string[]{
                "CAD USD 1.26",
                "USD CAD 0.79",
                "CAD EUR 1.07",
                "EUR CAD 0.93",
                "USD AUD 1.27",
                "AUD USD 0.79"};
    
            List<string> names = new List<string>();
    
            // First, look up all the names.
    
            foreach( var line in inputData )
            {
                string[] parts = line.Split(' ');
                if( !names.Contains(parts[0]) )
                    names.Add( parts[0] );
                if( !names.Contains(parts[1]) )
                    names.Add( parts[1] );
            }
    
            // Now make the array.
    
            table = new Dictionary<string, Dictionary<string, decimal>>();
            foreach( var n1 in names )
            {
                table[n1] = new Dictionary<string, decimal>();
                foreach( var n2 in names )
                {
                    table[n1][n2] = 0;
                }
                table[n1][n1] = 1;
            }
    
            // Now fill in the array.
    
            foreach( var line in inputData )
            {
                string[] parts = line.Split(' ' );
                string from = parts[0];
                string to = parts[1];
                decimal rate = Decimal.Parse( parts[2] );
    
                // Fill in the main conversion.
    
                table[from][to] = rate;
    
                // See if we can fill in any other conversions.
    
                foreach( var name in names )
                {
                    if( name == from || name == to )
                        continue;
                    // If I know CAD->EUR, and I already knew USD->CAD, then
                    // I can fill in USD->EUR.
    
                    if( table[name][to] == 0 && table[name][from] != 0 )
                        table[name][to] = table[name][from] * rate;
    
                    // If I know CAD->EUR and I already knew EUR->USD, then
                    // I can now fill in CAD->USD.
    
                    if( table[from][name] == 0 && table[to][name] != 0 )
                        table[from][name] = table[to][name] * rate;
                }
            }
    
            // Now I know all the rates.
    
            foreach( var row in table )
            {
                foreach( var col in row.Value )
                {
                    Console.WriteLine( "{0}->{1} = {2}",
                        row.Key, col.Key, col.Value
                    );
                }
            }
        }
    }
    
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by diegodamage82 Friday, July 13, 2018 2:31 AM
    Friday, July 13, 2018 12:37 AM
  • Perfect.

    I spent a lot of time without results.

    I really thank you.

    It works perfectly :)

    Thank you so much.

    Friday, July 13, 2018 2:34 AM