aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2021-02-12 09:44:12 +0100
committerJay Berkenbilt <ejb@ql.org>2021-02-13 08:30:00 +0100
commit07f40bd25442f25c0af948ae1b0dac7fdff1688c (patch)
tree3fca13d92b02bc3712d98c3c43f9612ae4c56cf1 /libqpdf/QUtil.cc
parent8fbc8579f2481dc3eeb962e99522047291e16fbe (diff)
downloadqpdf-07f40bd25442f25c0af948ae1b0dac7fdff1688c.tar.zst
QUtil::double_to_string: trim trailing zeroes with option to disable
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc25
1 files changed, 22 insertions, 3 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index 4b86c0d4..a3fa94f4 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -324,10 +324,17 @@ QUtil::uint_to_string_base(unsigned long long num, int base, int length)
std::string
QUtil::double_to_string(double num, int decimal_places)
{
+ return double_to_string(num, decimal_places, true);
+}
+
+std::string
+QUtil::double_to_string(double num, int decimal_places,
+ bool trim_trailing_zeroes)
+{
// Backward compatibility -- this code used to use sprintf and
// treated decimal_places <= 0 to mean to use the default, which
- // was six decimal places. Also sprintf with %*.f interprets the
- // length as fixed point rather than significant figures.
+ // was six decimal places. Starting in 10.2, we trim trailing
+ // zeroes by default.
if (decimal_places <= 0)
{
decimal_places = 6;
@@ -335,7 +342,19 @@ QUtil::double_to_string(double num, int decimal_places)
std::ostringstream buf;
buf.imbue(std::locale::classic());
buf << std::setprecision(decimal_places) << std::fixed << num;
- return buf.str();
+ std::string result = buf.str();
+ if (trim_trailing_zeroes)
+ {
+ while ((result.length() > 1) && (result.back() == '0'))
+ {
+ result.pop_back();
+ }
+ if ((result.length() > 1) && (result.back() == '.'))
+ {
+ result.pop_back();
+ }
+ }
+ return result;
}
long long