Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
c9d5a08e
Commit
c9d5a08e
authored
Jul 03, 2019
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the 'n' float format specifier
parent
f487ddbd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
2 deletions
+18
-2
include/fmt/format.h
include/fmt/format.h
+13
-1
test/format-test.cc
test/format-test.cc
+5
-1
No files found.
include/fmt/format.h
View file @
c9d5a08e
...
...
@@ -1097,6 +1097,9 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler&& handler) {
case
'A'
:
handler
.
on_hex
();
break
;
case
'n'
:
handler
.
on_num
();
break
;
default:
handler
.
on_error
();
break
;
...
...
@@ -1159,6 +1162,7 @@ class float_type_checker : private ErrorHandler {
FMT_CONSTEXPR
void
on_fixed
()
{}
FMT_CONSTEXPR
void
on_percent
()
{}
FMT_CONSTEXPR
void
on_hex
()
{}
FMT_CONSTEXPR
void
on_num
()
{}
FMT_CONSTEXPR
void
on_error
()
{
ErrorHandler
::
on_error
(
"invalid type specifier"
);
...
...
@@ -2642,9 +2646,14 @@ struct float_spec_handler {
bool
upper
;
bool
fixed
;
bool
as_percentage
;
bool
use_locale
;
explicit
float_spec_handler
(
char
t
)
:
type
(
t
),
upper
(
false
),
fixed
(
false
),
as_percentage
(
false
)
{}
:
type
(
t
),
upper
(
false
),
fixed
(
false
),
as_percentage
(
false
),
use_locale
(
false
)
{}
void
on_general
()
{
if
(
type
==
'G'
)
upper
=
true
;
...
...
@@ -2668,6 +2677,8 @@ struct float_spec_handler {
if
(
type
==
'A'
)
upper
=
true
;
}
void
on_num
()
{
use_locale
=
true
;
}
FMT_NORETURN
void
on_error
()
{
FMT_THROW
(
format_error
(
"invalid type specifier"
));
}
...
...
@@ -2728,6 +2739,7 @@ void internal::basic_writer<Range>::write_double(T value,
}
else
if
(
spec
.
align
()
==
ALIGN_DEFAULT
)
{
as
.
align_
=
ALIGN_RIGHT
;
}
// TODO: add thousands separators if handler.use_locale is set
if
(
use_grisu
)
{
auto
params
=
internal
::
gen_digits_params
();
params
.
fixed
=
handler
.
fixed
;
...
...
test/format-test.cc
View file @
c9d5a08e
...
...
@@ -1424,7 +1424,7 @@ TEST(FormatterTest, FormatFloat) {
}
TEST
(
FormatterTest
,
FormatDouble
)
{
check_unknown_types
(
1.2
,
"eEfFgGaA%"
,
"double"
);
check_unknown_types
(
1.2
,
"eEfFgGaA
n
%"
,
"double"
);
EXPECT_EQ
(
"0.0"
,
format
(
"{:}"
,
0.0
));
EXPECT_EQ
(
"0.000000"
,
format
(
"{:f}"
,
0.0
));
EXPECT_EQ
(
"0"
,
format
(
"{:g}"
,
0.0
));
...
...
@@ -1447,6 +1447,10 @@ TEST(FormatterTest, FormatDouble) {
EXPECT_EQ
(
buffer
,
format
(
"{:A}"
,
-
42.0
));
}
TEST
(
FormatterTest
,
FormatDoubleLocale
)
{
EXPECT_EQ
(
"1.23"
,
format
(
"{:n}"
,
1.23
));
}
TEST
(
FormatterTest
,
PrecisionRounding
)
{
EXPECT_EQ
(
"0"
,
format
(
"{:.0f}"
,
0.0
));
EXPECT_EQ
(
"0"
,
format
(
"{:.0f}"
,
0.01
));
...
...
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