diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-08-11 11:04:47 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-08-11 11:04:47 +0200 |
| commit | 7122475522a290e39fb32999efc5ed20aef0a315 (patch) | |
| tree | 8cfd61de6022471dad6275b137f52981a6639d92 /src | |
| parent | 2c43e00b12c9f26aad6f76e3a723bc231c9348b5 (diff) | |
| download | Project-Tick-7122475522a290e39fb32999efc5ed20aef0a315.tar.gz Project-Tick-7122475522a290e39fb32999efc5ed20aef0a315.zip | |
Print floating point number with the necessary precision
Diffstat (limited to 'src')
| -rw-r--r-- | src/text/json_formatter.cpp | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/text/json_formatter.cpp b/src/text/json_formatter.cpp index 7b73a26951..44acd61713 100644 --- a/src/text/json_formatter.cpp +++ b/src/text/json_formatter.cpp @@ -21,6 +21,8 @@ #include "nbt_tags.h" #include "nbt_visitor.h" #include <cmath> +#include <iomanip> +#include <limits> namespace nbt { @@ -49,13 +51,13 @@ namespace //anonymous void visit(const tag_float& f) override { - write_double(f.get()); + write_float(f.get()); os << "f"; } void visit(const tag_double& d) override { - write_double(d.get()); + write_float(d.get()); os << "d"; } @@ -159,13 +161,14 @@ namespace //anonymous os << indent_str; } - void write_double(double d) + template<class T> + void write_float(T val, int precision = std::numeric_limits<T>::max_digits10) { - if(std::isfinite(d)) - os << d; - else if(std::isinf(d)) + if(std::isfinite(val)) + os << std::setprecision(precision) << val; + else if(std::isinf(val)) { - if(std::signbit(d)) + if(std::signbit(val)) os << "-"; os << "Infinity"; } |
