Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
a5c51157
Commit
a5c51157
authored
Aug 26, 2015
by
Björn Reimer
Committed by
Wouter van Oortmerssen
Aug 26, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix conversion of floats when generating json
Change-Id: I01def42eda9b70308046c048099d85db8f889ede
parent
ac10873e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
3 deletions
+21
-3
util.h
include/flatbuffers/util.h
+21
-3
No files found.
include/flatbuffers/util.h
View file @
a5c51157
...
@@ -42,10 +42,8 @@ namespace flatbuffers {
...
@@ -42,10 +42,8 @@ namespace flatbuffers {
// Convert an integer or floating point value to a string.
// Convert an integer or floating point value to a string.
// In contrast to std::stringstream, "char" values are
// In contrast to std::stringstream, "char" values are
// converted to a string of digits.
// converted to a string of digits
, and we don't use scientific notation
.
template
<
typename
T
>
std
::
string
NumToString
(
T
t
)
{
template
<
typename
T
>
std
::
string
NumToString
(
T
t
)
{
// to_string() prints different numbers of digits for floats depending on
// platform and isn't available on Android, so we use stringstream
std
::
stringstream
ss
;
std
::
stringstream
ss
;
ss
<<
t
;
ss
<<
t
;
return
ss
.
str
();
return
ss
.
str
();
...
@@ -58,6 +56,26 @@ template<> inline std::string NumToString<unsigned char>(unsigned char t) {
...
@@ -58,6 +56,26 @@ template<> inline std::string NumToString<unsigned char>(unsigned char t) {
return
NumToString
(
static_cast
<
int
>
(
t
));
return
NumToString
(
static_cast
<
int
>
(
t
));
}
}
// Special versions for floats/doubles.
template
<>
inline
std
::
string
NumToString
<
double
>
(
double
t
)
{
// to_string() prints different numbers of digits for floats depending on
// platform and isn't available on Android, so we use stringstream
std
::
stringstream
ss
;
// Use std::fixed to surpress scientific notation.
ss
<<
std
::
fixed
<<
t
;
auto
s
=
ss
.
str
();
// Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
auto
p
=
s
.
find_last_not_of
(
'0'
);
if
(
p
!=
std
::
string
::
npos
)
{
s
.
resize
(
p
+
1
);
// Strip trailing zeroes.
if
(
s
.
back
()
==
'.'
)
s
.
pop_back
();
// Strip '.' if a whole number.
}
return
s
;
}
template
<>
inline
std
::
string
NumToString
<
float
>
(
float
t
)
{
return
NumToString
(
static_cast
<
double
>
(
t
));
}
// Convert an integer value to a hexadecimal string.
// Convert an integer value to a hexadecimal string.
// The returned string length is always xdigits long, prefixed by 0 digits.
// The returned string length is always xdigits long, prefixed by 0 digits.
// For example, IntToStringHex(0x23, 8) returns the string "00000023".
// For example, IntToStringHex(0x23, 8) returns the string "00000023".
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment