Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::format with thousands separator and negatives

Tags:

c++

I'm using "{:L}" to print locale-specific thousands separators when printing floating point numbers.

std::cout << std::format(std::locale("en_US.UTF-8"), "{:L}", 12345.678 );

Correctly produces:

12,345.678

However, I'm noticing some unexpected behavior from gcc when printing negative numbers.

std::cout << std::format(std::locale("en_US.UTF-8"), "{:L}", -123.45 );

Produces:

-,123.45

Am I wrong to expect -123.45, since there are no thousands? Does gcc have a bug?

Godbolt example

like image 239
Drew Dormann Avatar asked May 22 '26 06:05

Drew Dormann


1 Answers

The authoritative Jonathan Wakely has confirmed that this is currently a bug in libstdc++, known to fail in gcc 13.4, 14.3, 15.1

A leading + or - or in a formatted floating point number is mistakenly considered a digit.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120548

This is now reported as "Fixed" in gcc 13.5, 14.4 and 15.2.

like image 90
Drew Dormann Avatar answered May 24 '26 22:05

Drew Dormann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!