Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<format>: fix std::format("{:#.precision}", floating) #3815

Merged
merged 17 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -2864,7 +2864,7 @@ _NODISCARD _OutputIt _Fmt_write(

auto _To_upper = false;
auto _Format = chars_format::general;
auto _Exponent = '\0';
auto _Exponent = 'e';
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
auto _Precision = _Specs._Precision;

switch (_Specs._Type) {
Expand Down Expand Up @@ -3015,7 +3015,7 @@ _NODISCARD _OutputIt _Fmt_write(
_Zeroes_to_append = _Extra_precision;
break;
case chars_format::general:
if (_Specs._Alt) {
if (_Specs._Alt && (_Specs._Type == 'g' || _Specs._Type == 'G')) {
auto _Digits = static_cast<int>(_Exponent_start - _Buffer_start);

if (!_Append_decimal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_latest_matrix.lst
achabense marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
#include <format>

int main() {
assert(std::format("{:#.0}", 0.0) == "0.");
assert(std::format("{:#.1}", 0.0) == "0.");
assert(std::format("{:#.2}", 0.0) == "0.");

assert(std::format("{:#.0}", 1200.0) == "1.e+03");
assert(std::format("{:#.1}", 1200.0) == "1.e+03");
assert(std::format("{:#.2}", 1200.0) == "1.2e+03");
assert(std::format("{:#.3}", 1200.0) == "1.2e+03");
assert(std::format("{:#.4}", 1200.0) == "1200.");
assert(std::format("{:#.5}", 1200.0) == "1200.");
assert(std::format("{:#.6}", 1200.0) == "1200.");

assert(std::format("{:#.0}", 0.123) == "0.1");
assert(std::format("{:#.1}", 0.123) == "0.1");
assert(std::format("{:#.2}", 0.123) == "0.12");
assert(std::format("{:#.3}", 0.123) == "0.123");
assert(std::format("{:#.4}", 0.123) == "0.123");
assert(std::format("{:#.5}", 0.123) == "0.123");

assert(std::format("{:#.0}", 10.1) == "1.e+01");
assert(std::format("{:#.1}", 10.1) == "1.e+01");
assert(std::format("{:#.2}", 10.1) == "10.");
assert(std::format("{:#.3}", 10.1) == "10.1");
assert(std::format("{:#.4}", 10.1) == "10.1");
assert(std::format("{:#.5}", 10.1) == "10.1");
}
Copy link
Contributor Author

@achabense achabense Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only none type is affected by this pr, so there are no checks for other format types('g','a','f',etc).