point9408
point9408
CC#
Created by point9408 on 3/1/2023 in #help
❔ How to convert object type to double. Pls help me
[Route("/")]
public async Task<IActionResult> Index()
{
string stockSymbol = _options.DefaultStockSymbol;

if (string.IsNullOrEmpty(stockSymbol))
{
stockSymbol = "MSFT";
}


Dictionary<string, object> profile = await finnhubService.GetCompanyProfile(stockSymbol); //returns json data
Dictionary<string, object> price = await _finnhubService.GetStockPriceQuote(stockSymbol);

StockTrade stockTrade = new StockTrade
{
StockSymbol = stockSymbol,
StockName = profile.ContainsKey("name") ? profile["name"].ToString() : null,
//Price = price.ContainsKey("c") ? price["c"].GetDouble() : 0.0

};

return View(stockTrade);
}
[Route("/")]
public async Task<IActionResult> Index()
{
string stockSymbol = _options.DefaultStockSymbol;

if (string.IsNullOrEmpty(stockSymbol))
{
stockSymbol = "MSFT";
}


Dictionary<string, object> profile = await finnhubService.GetCompanyProfile(stockSymbol); //returns json data
Dictionary<string, object> price = await _finnhubService.GetStockPriceQuote(stockSymbol);

StockTrade stockTrade = new StockTrade
{
StockSymbol = stockSymbol,
StockName = profile.ContainsKey("name") ? profile["name"].ToString() : null,
//Price = price.ContainsKey("c") ? price["c"].GetDouble() : 0.0

};

return View(stockTrade);
}
In this example from controller, i get value of price as dictionary type and it has many values from external API . The first one is {[c, ValueKind = Number : "246.27"]} which i want to get that double number (246.27) and assign it to my Price variable. You see that I commented that line because it gives error. Is there any other way to achieve this?
21 replies