Write numbers using a standard formatter (15sf to match excel)

This commit is contained in:
Crzyrndm 2018-06-24 11:06:02 +12:00
parent f62e5fe5f7
commit 884558fd15

View File

@ -80,6 +80,16 @@ std::vector<std::pair<std::string, std::string>> core_property_namespace(xlnt::c
return {{constants::ns("core-properties"), "cp"}}; return {{constants::ns("core-properties"), "cp"}};
} }
std::string number_to_string(double num)
{
// any more digits and excel won't match
constexpr int Excel_Digit_Precision = 15;
std::stringstream ss;
ss.precision(Excel_Digit_Precision);
ss << num;
return ss.str();
}
} // namespace } // namespace
namespace xlnt { namespace xlnt {
@ -2359,7 +2369,8 @@ void xlsx_producer::write_worksheet(const relationship &rel)
if (props.width.is_set()) if (props.width.is_set())
{ {
write_attribute("width", (props.width.get() * 7 + 5) / 7); double width = (props.width.get() * 7 + 5) / 7;
write_attribute("width", number_to_string(width));
} }
if (props.best_fit) if (props.best_fit)
@ -2457,15 +2468,7 @@ void xlsx_producer::write_worksheet(const relationship &rel)
if (props.height.is_set()) if (props.height.is_set())
{ {
auto height = props.height.get(); auto height = props.height.get();
write_attribute("ht", number_to_string(height));
if (std::fabs(height - std::floor(height)) == 0.0)
{
write_attribute("ht", std::to_string(static_cast<int>(height)) + ".0");
}
else
{
write_attribute("ht", height);
}
} }
if (props.hidden) if (props.hidden)
@ -2588,19 +2591,7 @@ void xlsx_producer::write_worksheet(const relationship &rel)
case cell::type::number: case cell::type::number:
write_start_element(xmlns, "v"); write_start_element(xmlns, "v");
write_characters(number_to_string(cell.value<double>()));
if (is_integral(cell.value<double>()))
{
write_characters(static_cast<std::int64_t>(cell.value<double>()));
}
else
{
std::stringstream ss;
ss.precision(20);
ss << cell.value<double>();
write_characters(ss.str());
}
write_end_element(xmlns, "v"); write_end_element(xmlns, "v");
break; break;