@@ -71,26 +71,6 @@ void SmartString::trimBOM(std::string &string)
7171 }
7272}
7373
74- std::string SmartString::fromNumber (double value, NumberFormat format)
75- {
76- switch (format) {
77- case NumberFormat::prefixed: {
78- return humanReadable (value);
79- }
80- case NumberFormat::grouped: {
81- Conv::NumberToString converter;
82- converter.fractionDigitCount = 10 ;
83- converter.integerGgrouping = ' ' ;
84- return converter (value);
85- }
86- default :
87- case NumberFormat::none: {
88- Conv::NumberToString converter;
89- return converter (value);
90- }
91- }
92- }
93-
9474std::vector<std::string> SmartString::split (
9575 const std::string &str, char delim, bool ignoreEmpty)
9676{
@@ -139,15 +119,15 @@ std::vector<std::string> SmartString::split(const std::string &str,
139119 return result;
140120}
141121
142- std::string SmartString::humanReadable (double value, int digits,
143- const std::vector<std::string> &prefixes)
122+ std::string SmartString::fromNumber (double value, size_t digits)
144123{
145- Math::EngineeringNumber num (value);
124+ auto negative = value < 0 ;
125+ auto absValue = std::abs (value);
146126
147- std::string res = std::to_string (num. coefficient );
127+ std::string res = std::to_string (absValue );
148128
149- if (!Math::Floating (num. coefficient ).isInteger ()
150- && num. coefficient < pow (10 , digits - 1 ))
129+ if (!Math::Floating (absValue ).isInteger ()
130+ && absValue < pow (10 , digits - 1 ))
151131 {
152132 res = res.substr (0 , digits + 1 );
153133 }
@@ -163,15 +143,49 @@ std::string SmartString::humanReadable(double value, int digits,
163143 if (ch == ' .' ) break ;
164144 }
165145
166- if (!num.positive )
167- res = ' -' + res;
146+ if (negative) res = ' -' + res;
147+
148+ return res;
149+ }
150+
151+ std::string SmartString::fromNumber (
152+ double value,
153+ NumberFormat format,
154+ size_t maxFractionDigits)
155+ {
156+ switch (format) {
157+ case NumberFormat::prefixed: {
158+ return humanReadable (value, maxFractionDigits);
159+ }
160+ case NumberFormat::grouped: {
161+ Conv::NumberToString converter;
162+ converter.fractionDigitCount = maxFractionDigits;
163+ converter.integerGgrouping = ' ' ;
164+ return converter (value);
165+ }
166+ default :
167+ case NumberFormat::none: {
168+ Conv::NumberToString converter;
169+ converter.fractionDigitCount = maxFractionDigits;
170+ return converter (value);
171+ }
172+ }
173+ }
174+
175+ std::string SmartString::humanReadable (double value, int maxFractionDigits,
176+ const std::vector<std::string> &prefixes)
177+ {
178+ Math::EngineeringNumber num (value);
179+
180+ std::string res = fromNumber (num.coefficient ,
181+ NumberFormat::none, maxFractionDigits);
168182
169183 if (num.exponent >= 0 && num.exponent < (int )prefixes.size ())
170184 {
171185 auto prefix = prefixes.at (num.exponent );
172186 return res + (!prefix.empty () ? " " + prefix : " " );
173187 }
174- else return fromNumber (value);
188+ else return fromNumber (value, NumberFormat::none, maxFractionDigits );
175189}
176190
177191std::string SmartString::deescape (const std::string &str)
0 commit comments