Skip to content

Commit

Permalink
nall: set numeric locale to "C" at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeUsher committed Jun 19, 2024
1 parent 8c4af6a commit 3372230
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 10 deletions.
6 changes: 1 addition & 5 deletions nall/atoi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ constexpr inline auto toInteger(const char* s) -> s64 {
//

inline auto toReal(const char* s) -> f64 {
const char* locale = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "C");
auto result = atof(s);
setlocale(LC_NUMERIC, locale);
return result;
return atof(s);
}

}
2 changes: 2 additions & 0 deletions nall/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ auto main(int argc, char** argv) -> int {
_setmode(_fileno(stderr), O_BINARY);
#endif

setlocale(LC_NUMERIC, "C"); // Enforce 'standard' locale for numeric formatting

main(Arguments{argc, argv});

#if !defined(PLATFORM_WINDOWS)
Expand Down
5 changes: 0 additions & 5 deletions nall/string/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ template<typename T> inline auto fromHex(char* result, T value) -> char* {
//a double to a string ... but attempting to parse a double by
//hand, digit-by-digit, results in subtle rounding errors.
template<typename T> inline auto fromReal(char* result, T value) -> u32 {
const char* locale = setlocale(LC_NUMERIC, NULL);
setlocale(LC_NUMERIC, "C");

char buffer[256];
#ifdef _WIN32
//Windows C-runtime does not support long double via sprintf()
Expand All @@ -169,8 +166,6 @@ template<typename T> inline auto fromReal(char* result, T value) -> u32 {
snprintf(buffer, sizeof(buffer), "%Lf", (long double)value);
#endif

setlocale(LC_NUMERIC, locale);

for(char* p = buffer; *p; p++) {
//remove excess 0's in fraction (2.500000 -> 2.5)
if(*p == '.') {
Expand Down

0 comments on commit 3372230

Please sign in to comment.