mercredi 25 mars 2020

Best pattern for converting between potentially lossy types, ie Coordinate in DMS

So I'm working on a program that uses coordinates in two formats, decimal and degrees minutes and seconds. I have a requirement to allow the user to convert between the two on a drop down.

All of this is fine, but I started to think that I could use a pattern of some sort to store which version was originally entered and is the 'truth', and which is the lossy one. However this would need to switch if someone entered 10.123 in decimal, converted it to DMS as 10:07:23 (where the seconds would be 22.8 if using a decimal).

Just for context, the conversion is below, due to the divisor containing a factor of 3, its going to be inherently lossy.

double decimal = 10.123;
int degrees = (int) Math.floor(decimal);
int minutes = (int) Math.floor((decimal - degrees) * 60d);
int seconds = (int) Math.round(((decimal - degrees) * 60d - minutes) * 60d);

I feel like the observer pattern may be the best way to start, but I'll be the first to admit my knowledge of patterns is limited.

Happy to provide more context if needed.

Aucun commentaire:

Enregistrer un commentaire