Format woes

1
2
3
int main() {
	std::cout <<  std::format("{:< 8f}", .111111);
}


displays 0.111111 but I need .111111 (no initial 0). Is this possible just using format or do I need to manipulate the produced string?
With a quick 'net search using multiple search terms I'd have to opine there is no built-in method to not have that leading zero, in std::format or printf.

Interestingly coincidental many of the results showed how to add leading zeros.

You could create a custom formatter, essentially treating your decimal number as a custom type.

https://www.cppstories.com/2022/custom-stdformat-cpp20/

That looks to be a lot more work than I'd care to do. Whacking off any leading zeros by manipulating the string is probably the quickest and simplest route to go.

Creating a custom formatter is quite beyond my self-taught hobbyist capabilities currently. I'd have to spend a lot of time with a hammer, blow-torch and crowbar 55 gallons of lubricant, 40 feet of rubber tubing and a Yak to do some tests on creating one.

My current interests are ones that lie in different directions. Eventually I might wander back and look at creating custom formatters, when dealing with custom class output.
Whacking off any leading zeros by manipulating the string is probably the quickest and simplest route to go.


That's what I've done. But I was interested if there was a way of doing it just by using a a format string. It seems not...

You could create a custom formatter ... That looks to be a lot more work than I'd care to do


Yeah - but IMO that's massive over-engineering for the simple issue in this case.
Last edited on
I'm more than likely just not understanding things but maybe you could try std::format_to or std::format_to_n?

https://en.cppreference.com/w/cpp/utility/format/format_to
https://en.cppreference.com/w/cpp/utility/format/format_to_n

TBH I look at those examples and to me they are as complicated as writing a custom formatter from scratch. Nigh incomprehensible gobbledygook and argyle-gargle flummery.
format_to sends it's output to an output buffer iterator rather than returning a string. format-to_n is format_to with the maximum number of characters to be written to the buffer also specified. They both use the same format specifiers as format.
How about a little mess of code like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

int main()
{
  
  double a = 0.110000;
  
  
  if(to_string(a)[0] == '0') {
    for(int i = 1; i < to_string(a).length(); i++) {
      cout << to_string(a)[i];   
    }
  }
  else {
     cout << to_string(a);   
  }
 
  return 0;
}
Last edited on
If you're going to write less code then write less code.
1
2
auto s = std::to_string(a);
std::cout << (s.size() > 0 && s[0] == '0' ? s.substr(1) : s);
ok less code...

1
2
auto s = std::to_string(a);
std::cout << (s.size() && s[0] == '0' ? s.substr(1) : s);


I win...

 
std::cout << s.substr(s.size() && s[0] == '0');

std::cout << s.substr(*s.c_str() == '0');
Topic archived. No new replies allowed.