1

Here's a related topic.

I'm looking for something slightly different, I'm printing out a 4x4 floating point matrix. In fact I'm using this prettyprint method to do it, which will preclude me from using printf formatting which I'm a little more familiar with.

I can set ios_base::width() to set a minimum width to pad out the zeros and ones but the likes of 0.6666667 screws up the alignment. I figure I can set precision but it only affects the number of significant figures and not the total number of char's produced by the value. I can't seem to use unsetf on an ostream to set 'default notation'... Even if I could do that I suspect a minus sign would mess up alignment anyway.

Is there a workaround?

Community
  • 1
  • 1
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • What do you intend to do if you limit yourself (e.g.) to 4 characters and someone wants to use (e.g.) 55432(.123456)? It may be ok to lose precision, which buys you space after the decimal point, but not before. Maybe you could work out the widest you need, then pad the others out to that? – BoBTFish Feb 21 '12 at 15:59
  • Ideally I'd just like to print out the left-most n characters: n=4: 0.004 or 243.2 or -222. if I have a value bigger or smaller than can be expressed this way, then I don't need to know what the exact value is. It seems like I may need to do some custom string handling to get the exact behavior i'd want... I was able to get `setf(std::ios_base::fixed)` to work which is better than before but I do have the minus sign issue, and like you said, it doesnt count digits coming before the decimal point. Am I to assume there is no mechanism for me to limit width? But it's possible with `printf`... – Steven Lu Feb 21 '12 at 16:03
  • I'm looking at `std::setprecision` from `` now. Nope, it looks like that does nothing other than call `precision` more conveniently. I also seem to be wrong about `printf` being able to do this, too. It doesn't allow me to truncate! So the minus sign goes along with the other preceding digits as something I have to deal with on my own. Somebody please make an answer to give a solution (other than the straightforward `sprintf` hackery) or to verify that there is no stdlib solution as I suspect now. – Steven Lu Feb 21 '12 at 16:07
  • So if you limit to 4 characters and someone enters 56789 you want it to print the left most 4 characters: 5678 - is that correct? Perhaps you can push each one onto a string stream, padding the shorter numbers up to your desired size, then pull off just the amount you need. – BoBTFish Feb 21 '12 at 16:11

1 Answers1

1

My temporary solution is width(7) and precision(4). It gives me 4 digits after the decimal point and 3 which is space to put the minus sign, a single digit, and the decimal point. I have to check and modify width in order to accomodate (some of the) values larger than 99.9999 or less than -9.9999 to preserve alignment. This has the benefit that 567890123 will not be printed as 5678901, all it will do is throw off alignment (a lot, it will be printed as 567890123.0000).

Steven Lu
  • 41,389
  • 58
  • 210
  • 364