-3

I would think this would be an oft-asked question, but could not find its duplicate. Googling didn't help, either.

Why does a double display as an int in the visual studio debugger, as shown below:

enter image description here

The value should be around 4.4 or so. Why does Visual Studio just show a rounded version of the value? More importantly, how can I get it to show a real value rather than an approximation?

stuartd
  • 70,509
  • 14
  • 132
  • 163
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 5
    Because of integer division. One int divided by another will always give you an int result. This might help: https://stackoverflow.com/questions/661028/how-can-i-divide-two-integers-to-get-a-double – itsme86 Jun 12 '20 at 20:25
  • 1
    There is not enough information in this question to answer it. Can you clarify which variable you're asking about, and also show the code that assigns a value to that variable, as well as the types of all the variables and expressions and properties involved. As @itsme86 says, it might be because of integer types being involved, but your breakpoint is on a line assigning a value to `iWordsInDoc2`, and your highlighted watch is on a value of the variable `iCountOfLettersInDoc2`. There is no way to answer your question unless you clarify **what** you're asking about. – Lasse V. Karlsen Jun 12 '20 at 20:33
  • @LasseV.Karlsen: The values in the Watch window tell the story, I think. Simple deal, I just forgot about it (been away from programming for years, and am working on a personal "hobby" project). – B. Clay Shannon-B. Crow Raven Jun 12 '20 at 20:35
  • 1
    Unless I see your code declaring `iCountOfLettersInDock2` as a double, then I am inclined to believe you're just mistaken. I'm also questioning your claim that "count of letters in document 2" is 4.4. How can you have 4.4 letters in a document? – Lasse V. Karlsen Jun 12 '20 at 20:36
  • Or have you shown code with a breakpoint on X, highlighting a value on Y, and actually asking about Z? Aka, you have a breakpoint on assigning to `iWordsInDoc2`, you have shown a watch highlighting `iCountOfLettersOInDoc2`, but you're actually asking about `LettersPerWordInDoc2`? – Lasse V. Karlsen Jun 12 '20 at 20:37
  • @LasseV.Karlsen The "i" vars are integers, but WordsPerSentenceInDoc2 and LettersPerWordInDoc2 are doubles. The 4.4 letters are an average, as are the words per sentence. – B. Clay Shannon-B. Crow Raven Jun 12 '20 at 20:38
  • 1
    Well, then see the current answer. Dividing an integer by another integer gives an integer result. Next time, also, be **clear** about what your code is portraying. – Lasse V. Karlsen Jun 12 '20 at 20:39
  • Please... Don't use Hungarian notation in c#. Obviously the choice is yours to make but it goes against most conventions. The name should indicate the purpose of the variable, not its type. If you ever have to change the type, now you've got to litter your diffs with (ie) `iSomething`->`dSomething` instead of just the declaration itself – pinkfloydx33 Jun 12 '20 at 20:50
  • @pinkfloydx33: I've always found that Hungarian helps me; I try to also make the names meaningful so what it is and what it is for are clear (to me, anyway). To some, Hungarian is just goulash, but I like it. – B. Clay Shannon-B. Crow Raven Jun 12 '20 at 22:36

1 Answers1

1

It's as simple as the double in actuality holding an integer due to the fact that two integers divided against each other give an int result. To get the wanted double value, the ins must be cast to doubles like so:

double WordsPerSentenceInDoc2 = 0.0;
double LettersPerWordInDoc2 = 0.0;
. . .
WordsPerSentenceInDoc2 = (double)iWordsInDoc2 / (double)iCountOfSentencesInDoc2;
LettersPerWordInDoc2 = (double)iCountOfLettersInDoc2 / (double)iWordsInDoc2;
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862