Answer the question
In order to leave comments, you need to log in
Is it possible to cast long/double to float? Or long in double C#?
I get json via api and convert it to a structure, but it turns out nested arrays and I need to convert it to the class I need. But the data comes sometimes double sometimes long. And it is not possible to cast both float and double to a common type. Now I left float as it is described in the API doc that they should be float, but the json parser defines them as double. Here is a dock for everyone https://opensky-network.org/apidoc/rest.html.
Here is a screenshot of the structure, where I highlighted the same field. It happens double happens long.
This is a class so that he knows how to convert json
public class AirData
{
public string icao24;
public string callsign;
public string origin_country;
public long time_position;
public long last_contact;
public float longitude;
public float latitude;
public float baro_altitude;
public bool on_ground;
public float velocity;
public float true_track;
public float vertical_rate;
public object sensors;
public float geo_altitude;
public string squawk;
public bool spi;
public long position_source;
}
public List<AirData> ConvertToAirData()
{
var temp = new List<AirData>();
foreach (var notformatted in states)
{
var airData = new AirData();
airData.icao24 = (string) notformatted[0];
airData.callsign = (string) notformatted[1];
airData.origin_country = (string) notformatted[2];
airData.time_position = (long) notformatted[3];
airData.last_contact = (long) notformatted[4];
airData.longitude = (float) notformatted[5];
airData.latitude = (float) notformatted[6];
airData.baro_altitude = (float) notformatted[7];
airData.on_ground = (bool) notformatted[8];
airData.velocity = (float) notformatted[9];
airData.true_track = (float) notformatted[10];
airData.vertical_rate = (float) notformatted[11];
airData.sensors = notformatted[12];
airData.geo_altitude = (float) notformatted[13];
airData.squawk = (string) notformatted[14];
airData.spi = (bool) notformatted[15];
airData.position_source = (long) notformatted[16];
temp.Add(airData);
}
return temp;
}
Answer the question
In order to leave comments, you need to log in
Thanks everyone for the replies, while the problem was solved by replacing (double) with Convert.ToDouble(). Now I'm going to test further. Can someone tell me what is the difference between (double) and Convert.ToDouble(), otherwise the problem was solved, but I don’t understand how. They also suggested Convert.ToSingle() - it's also interesting how it works, I didn't understand from the docks.
Why not just deserialize to AirData? and not try to mapit hands.
For manual mapping, you can use Convert.ToSingle(notformatted[7])
The API describes that they should be floats, but the json parser defines them as doubleThere is an opinion that he does not know about the API, so for a number with a dot he suggests double, without a dot - long
The data, in fact, comes in the form of text (json), which the parser interprets either assuming what and how, or "looking back" at the structure of where it is deserialized ...
Well, var_value: 1 - this can be anything within the framework of guesswork - starting from bit fields and through dozens of variations of signed|unsigned int to various variants of decimal, float, etc.
Real types are already from the documentation, or at least from the presentation of the subject area.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question