Skip to content

Commit

Permalink
Fix writing floating point numbers when std::locale::global is set (#174
Browse files Browse the repository at this point in the history
)

If the global locale is set to something not using '.' in floating point numbers, it would result in invalid E57 XML.
  • Loading branch information
asmaloney committed Nov 7, 2022
1 parent ba38b58 commit 4fd86d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ jobs:
sudo apt-get install -y libxerces-c-dev ninja-build
sudo locale-gen fr_FR
- name: Cache miniconda (Windows)
if: matrix.config.os == 'windows-latest'
uses: actions/cache@v3
env:
CACHE_NUMBER: 0 # Increase this value to reset cache if environment.yml has not changed
with:
path: ~/conda_pkgs_dir
key:
${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('.github/conda/environment.yml') }}
key: ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('.github/conda/environment.yml') }}

- name: Setup miniconda (Windows)
if: matrix.config.os == 'windows-latest'
Expand Down
2 changes: 2 additions & 0 deletions src/StringFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace e57
static_assert( std::is_floating_point<FTYPE>::value, "Floating point type required." );

std::stringstream ss;
ss.imbue( std::locale::classic() );

ss << std::scientific << std::setprecision( precision ) << value;

// Try to remove trailing zeroes and decimal point
Expand Down
24 changes: 24 additions & 0 deletions test/src/test_StringFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// libE57Format testing Copyright © 2022 Andy Maloney <asmaloney@gmail.com>
// SPDX-License-Identifier: MIT

#include <clocale>

#include "gtest/gtest.h"

#include "StringFunctions.h"
Expand All @@ -25,3 +27,25 @@ TEST( StringFunctions, FloatToStrConversion2 )

ASSERT_EQ( converted, "1.2345600e+05" );
}

// Related to https://github.com/asmaloney/libE57Format/issues/172
// Floating point should always use '.'.
TEST( StringFunctions, FloatToStrLocale )
{
try
{
std::locale::global( std::locale( "fr_FR" ) );
}
catch ( std::exception &err )
{
GTEST_SKIP() << "fr_FR locale not available: " << err.what();
}

std::wcout.imbue( std::locale() );

const auto converted = e57::floatingPointToStr<float>( 123456.0f, 7 );

ASSERT_EQ( converted, "1.2345600e+05" );

std::locale::global( std::locale::classic() );
}

0 comments on commit 4fd86d9

Please sign in to comment.