I have a Linux system that is set to some locale and its running a c++ application. I can do a std::setlocale(LC_NUMERIC, "en_US.UTF-8")
from a c++ application or from OS itself (by changing at /etc/default/locale
). I don't have access to main function, so i did std::setlocale(LC_NUMERIC, "en_US.UTF-8")
in my function and it works fine.
However, i would like to do it an object level or at some global level (once) in my application. I looked quite a bit on internet but didn't find much help, so decided to ask. Here is my struct for GPS Position
So, basically
//struct Position : std::numpunct<char> {
struct Position {
Position() { isValid = PR_FALSE; lat = 0; lng = 0; elevation = 0; };
PRBool isValid;
double lat;
double lng;
double elevation;
//char do_decimal_point() const { return '.'; } // separate with slash
};
And then, in my function i do
mycheckposition(){
Position checkPosition;
Position mPosition;
// get some data in above.
double distance;
GetDistanceBetweenWGS84Coords(checkPosition, mPosition, distance);
}
My question is where should i do setlocale effectively and safely. My Position values gets comma instead of dot and then GetDistanceBetweenWGS84Coords fails. The LC_NUMERIC on my system represents comma for number with decimal. May be, if someone wants to see GetDistance function, here it is.
int GetDistanceBetweenWGS84Coords(const Position& from, const
Position& to, double& distance)
{
const double EARTH_RADIUS_IN_METERS = 6372797.560856;
const double DEG_TO_RAD = 0.017453292519943295769236907684886;
double latitudeArc = (from.lat - to.lat) * DEG_TO_RAD;
double longitudeArc = (from.lng - to.lng) * DEG_TO_RAD;
double latitudeH = sin(latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = sin(longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = cos(from.lat*DEG_TO_RAD) * cos(to.lat*DEG_TO_RAD);
double arcInRadians = 2.0 * asin(sqrt(latitudeH + tmp*lontitudeH));
distance = EARTH_RADIUS_IN_METERS * arcInRadians;
return 0; //success
}
I did found something that solves my problem but haven't managed to get this working. Not sure if description in this link follows std lib.