Setting precision for double results in exponencial notation #435
-
Consider the following code that calls text().set() on a double with different precisions: pugi::xml_document doc;
auto doc_node = doc.append_child("Numbers");
double d = 30000.001;
for (int precision = 0; precision < 7; ++precision)
{
auto num_node = doc_node.append_child("number");
num_node.append_attribute("precision") = precision;
num_node.text().set(d, precision); // <<<
}
doc.save(std::cout, " "); The output is: <?xml version="1.0"?>
<Numbers>
<number precision="0">3e+04</number>
<number precision="1">3e+04</number>
<number precision="2">3e+04</number>
<number precision="3">3e+04</number>
<number precision="4">3e+04</number>
<number precision="5">30000</number>
<number precision="6">30000</number>
</Numbers> Note that up to precision 4 the number is printed in scientific notation. Is this intended? Is there a way to force printing of floating point numbers in standard notation? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
pugixml simply uses sprintf with %g specifier to format numbers, so that's the behavior there - it switches between scientific and regular notations based on the estimated output length. If you want consistent non-scientific output you can format the numbers externally using sprintf or stringstream, and then use |
Beta Was this translation helpful? Give feedback.
pugixml simply uses sprintf with %g specifier to format numbers, so that's the behavior there - it switches between scientific and regular notations based on the estimated output length. If you want consistent non-scientific output you can format the numbers externally using sprintf or stringstream, and then use
set
withconst char*
argument instead ofdouble
.