Skip to content

Commit

Permalink
Formatting double is now pure function that return string instead of …
Browse files Browse the repository at this point in the history
…passing oss.
  • Loading branch information
vidanovic committed Oct 22, 2024
1 parent 169d8f0 commit 40efe42
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ endif()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

set(LIB_MAJOR_VERSION "1")
set(LIB_MINOR_VERSION "1")
set(LIB_PATCH_VERSION "0")
set(LIB_MINOR_VERSION "0")
set(LIB_PATCH_VERSION "1")
set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}")

set(LIB_NAME ${PROJECT_NAME})
Expand Down
6 changes: 2 additions & 4 deletions include/fileParse/Base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,9 @@ namespace FileParse
inline NodeAdapter & operator<<(NodeAdapter & node, double value)
{
const auto & config{SerializationConfig::getInstance()};
std::ostringstream oss;
FileParse::formatDouble(
oss, value, config.precision, config.scientificLowerBound, config.scientificUpperBound);

node.addText(oss.str());
node.addText(FileParse::formatDouble(
value, config.precision, config.scientificLowerBound, config.scientificUpperBound));

return node;
}
Expand Down
27 changes: 14 additions & 13 deletions include/fileParse/Formatter.cxx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <sstream>

#include "Formatter.hxx"

namespace FileParse
Expand Down Expand Up @@ -36,28 +38,27 @@ namespace FileParse
}
}

void formatDouble(std::ostringstream & oss,
double value,
std::string formatDouble(double value,
int precision,
double scientificLowerBound,
double scientificUpperBound)
{
bool useScientific
= (std::abs(value) < scientificLowerBound || std::abs(value) > scientificUpperBound)
&& value != 0.0;
bool useScientific = (std::abs(value) < scientificLowerBound || std::abs(value) > scientificUpperBound)
&& value != 0.0;

std::ostringstream tempStream;
tempStream.setf(useScientific ? std::ios::scientific : std::ios::fixed, std::ios::floatfield);
tempStream.precision(precision);
tempStream << value;

oss.setf(useScientific ? std::ios::scientific : std::ios::fixed, std::ios::floatfield);
oss.precision(precision);
oss << value;
std::string str = tempStream.str(); // Get the formatted string

std::string str = oss.str();
if(str.find('.') != std::string::npos)
// Trim trailing zeros if there is a decimal point in the string
if (str.find('.') != std::string::npos)
{
trimTrailingZeros(str, useScientific);
}

// Reassign trimmed string to the ostringstream
oss.str(str);
oss.clear();
return str; // Return the final formatted string
}
} // namespace FileParse
11 changes: 5 additions & 6 deletions include/fileParse/Formatter.hxx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include <sstream>
#include <string>

namespace FileParse
{
extern void formatDouble(std::ostringstream & oss,
double value,
int precision,
double scientificLowerBound = 0.001,
double scientificUpperBound = 100000);
extern std::string formatDouble(double value,
int precision,
double scientificLowerBound = 0.001,
double scientificUpperBound = 100000);
}

0 comments on commit 40efe42

Please sign in to comment.