I have a problem with conversion from double to decimal:
public class CartesianCoordinates
{
public int LatitudeHours { get; set;}
public int LatitudeMinutes { get; set; }
public int LatitudeSeconds { get; set; }
public GeoDirectionLongtitude LongitudeDirection { get; set; }
public int LongitudeHours { get; set; }
public int LongitudeMinutes { get; set; }
public int LongitudeSeconds { get; set; }
public GeoDirectionLatitude LatitudeDirection { get; set; }
}
public class DecimalCoordinates
{
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
CartesianCoordinates CartesianCoordinates=new CartesianCoordinates(){LatitudeHours =12,LatitudeMinutes =34,LatitudeSeconds=56 }
converterDecimalCoordinates.Latitude = CartesianCoordinates.LatitudeHours + (CartesianCoordinates.LatitudeMinutes + (CartesianCoordinates.LatitudeSeconds / 60)) / 60;
Why I get 12 ? I want 12,55
As a byproduct of my discussion with David M and Daniel Brückner under this answer and the partially wrong statement by myself under this answer by Adam, it has become clear that, sorry to say, all answers are only partially correct. What is happening is this:
The result is: that just adding a
60Mor60.0to the whole statement, as has been suggested, will not (or may not) yield the wanted result, depending on execution order of the statement and/or the existence of addition / subtraction, as is the case in the OP’s question.To fix this, follow Adam’s advice and convert each addition / subtraction step to decimals, use decimals all along (not very clear) or place the calculation in a little function that takes decimals as parameters, forcing implicit conversion:
which, as a bonus, is shorter and adds to readability. Call this with the following statement: