货币转换问题

时间:2017-09-15 14:33:14

标签: asp.net-mvc c#-4.0

我有类帮助将usd转换为inr现在正在给出错误,这就是:

  

指定的参数超出了有效值的范围。

在此行代码行中:

var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;

这是一个完整的课程:

public static decimal CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
{
    //Grab your values and build your Web Request to the API
    string apiURL = String.Format("http://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString());
    //Make your Web Request and grab the results
    var request = WebRequest.Create(apiURL);
    //Get the Response
    var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
    //Grab your converted value (ie 2.45 USD)
    var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;
    //Get the Result
    return Convert.ToDecimal(result.Replace(" INR", ""));
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您使用的网址不正确。它会导致重定向到span,这意味着它没有执行转换。如果您查看返回的流数据,则它没有与您的正则表达式匹配的匹配http://finance.google.com。如果您设置了网址static void Main(string[] args) { int amount = 100; string from = "USD"; string to = "INR"; var result = CurrencyConvert(amount, from, to); Console.WriteLine($"Conversion of {amount} from {from} to {to} equals {result}"); } public static decimal CurrencyConvert(decimal amount, string fromCurrency, string toCurrency) { //Grab your values and build your Web Request to the API string apiURL = String.Format("http://finance.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString()); //Make your Web Request and grab the results var request = WebRequest.Create(apiURL); //Get the Response var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.ASCII); //Grab your converted value (ie 2.45 USD) var streamData = streamReader.ReadToEnd(); var result = Regex.Matches(streamData, "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value; //Get the Result return Convert.ToDecimal(result.Replace(" INR", "")); } ,则该网址应该有效。

import dashboardIcon from '../../images/icons/Dashboard-Icon.svg'

<object className={classes.navIcon} type="image/svg+xml" data={dashboardIcon} 
alt='' />