-2

Does anyone know why this Format command produces the following strange results:

textbox1.Text = String.Format("{0:+;-}{0,7:0.000;0.000}", dblVariable);

Here are the strange results (check the signs of the formatted text - the sign is incorrect for numbers smaller than 0.5):

dblVariable       textbox1.Text
-0.100000         + 0.100
-0.200000         + 0.200
-0.300000         + 0.300
-0.400000         + 0.400
-0.500000         - 0.500

Thanks

NESHOM
  • 899
  • 16
  • 46
  • 1
    Like dotnetom said, what is so strange about the results? That's probably why your question was down voted. You need to provide more info. -1 for assuming we are mind readers :) (I didn't actually down vote). – rory.ap Feb 19 '15 at 20:40
  • I modified the question! – NESHOM Feb 19 '15 at 20:41
  • 1
    Okay, here's my advice. Modify your question so that is *plainly clear what the problem is*. Don't just say "check the signs of the formatted text"; say something like "The doubles all have a negative value but the formatted output is showing a `+` sign in most cases". Don't make it a vague guessing game for people. – rory.ap Feb 19 '15 at 20:43
  • I've been unable to reproduce the issue. [IDEOne](https://ideone.com/jwbwpF) . can you create a small demo that does show the problem? – Sayse Feb 19 '15 at 20:45
  • With your code, I get the exact same strange result on my system. (+ 0.100 + 0.200 + 0.300 + 0.400 - 0.500) – NESHOM Feb 19 '15 at 20:50
  • @NESHOM - I copy and pasted that from your example to show that it works fine. Have you stepped through and verified the value of `dblVariable`? – Sayse Feb 19 '15 at 20:54
  • Yes, I created a new project, copy-pasted your piece of code to a button event and it gives me the exact same strange result! – NESHOM Feb 19 '15 at 21:02
  • @Sayse - based on dotnetom's answer, I wonder how did you get the right output using my code?! – NESHOM Feb 19 '15 at 21:23
  • @NESHOM - IDEOne uses the mono compiler I believe, did you see the std out in that link? – Sayse Feb 20 '15 at 07:13

1 Answers1

2

The article in MSDN describes the issue. Look at what is said next to Two sections:

If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, the resulting zero is formatted according to the first section.

In your case the format of the sign is {0:+;-}. This effectively means that there is no number format, just sign format. So when rounding to this format it must be rounded to a integer number. So in case of -0.1 to -0.4 the number is rounded to 0, which uses first section (+), but -0.5 is rounded to -1, so second section (-) is used.

You can fix it by only using single format:

textbox1.Text = String.Format("{0,7:+ 0.000;- 0.000}", dblVariable)
dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • Thank you, but this is not exactly what I want. I have several textbox controls that are vertically aligned. I want them all to show the values right-aligned with the floating points also vertically aligned and they also show signs. Here is my original question http://stackoverflow.com/q/27511347/3179989. Any solution? – NESHOM Feb 19 '15 at 21:09
  • I updated answer to use `{0,7:...` instead of `{0:...` – dotnetom Feb 19 '15 at 21:11
  • Thank you, but this still does not make the floating points vertically aligned . Please check my question where I listed an example of what I am looking for http://stackoverflow.com/q/27511347/3179989 – NESHOM Feb 19 '15 at 21:18
  • So no way to do this? – NESHOM Feb 19 '15 at 21:44
  • 1
    In that case - no. However the answer explains why you get `+` where you expect to get `-` – dotnetom Feb 20 '15 at 05:00
  • @NESHOM - You need to use a monospaced font if you wish for the text to be aligned also – Sayse Feb 20 '15 at 07:14