Take the 2-minute tour ×

This double: 16.8999999

after theDouble = Math.Round(theDouble, 1) it shows up in the debugger as 16.9 but in the UI later on it's NOT 16.9 but 16.899999.

Why is this so?

I am developing a WP7 application and however and whenever I round a certain value, it always shows up "derounded" in the UI. I have even tried to make one last round before assigning the array to the listbox's itemssource. It's really strange and I'd appreciate any help or explanation.

I have even tried to assign a value myself just before the itemssource gets set. I assign 16.89999 and round it using the above method. In the debugger I get the expected result but then in the UI I get another result, namely, 16.899999618. Help?

share|improve this question
possible duplicate of Why does (int)(33.46639 * 1000000) return 33466389? –  Michael Petrotta Jun 18 '11 at 2:39
yeah, sorry about the title, I was just so baffled as to why it doesn't work. –  Phil Jun 19 '11 at 17:18
Wow, I must have hit a sore spot with my title. Three downvotes! Personal record. –  Phil Jun 19 '11 at 17:21
Do I still deserve the downvotes? It's a pretty tricky problem. –  Phil Jun 19 '11 at 19:49
add comment

5 Answers

Assign the value back to the variable:

   mydouble = Math.Round(mydouble, 1);


Since you updated your question, you can focus on how the value is displayed instead.

share|improve this answer
I am doing that. That's what's surprising. –  Phil Jun 19 '11 at 17:19
you're seeing this in the debugger probably. Floating point numbers are always imprecise. Lean on how you present the value instead of worrying about it's "real" value as stored in memory. –  Adam Dymitruk Jun 19 '11 at 17:43
No, I am not. Everything is fine and dandy until it gets presented in the UI. –  Phil Jun 19 '11 at 18:21
Instead of round, use the string formatting in .net to do the rounding. –  Adam Dymitruk Jun 19 '11 at 18:29
I'm assigning objects to the ItemsSource, any suggestion on how I would do what you suggested? –  Phil Jun 19 '11 at 18:31
add comment

Um, no: http://ideone.com/zuK9Z

I think you've made a mistake in your code. Are you doing something like this:

double theDouble = 16.8999999;


It should be:

double theDouble = 16.8999999;
theDouble = Math.Round(theDouble);
share|improve this answer
What's the downvote for? –  minitech Jun 19 '11 at 20:12
add comment
up vote 1 down vote accepted

I have no idea but it could have something to do with how WP7 presents doubles in datacontexts. I changed the type to float and now it works perfectly. I hope someone in the same situation finds this question even though it's pretty downvoted.

share|improve this answer
That it works for float and not for double is purely a fluke. You'll see different results for other numbers. You should display your code with a format string if you at all can; then make the format string round properly (e.g., %.1f). –  Chris Jester-Young Jun 20 '11 at 12:49
I guess I should ask another question on how to display values when the datacontext is bound to objects. –  Phil Jun 20 '11 at 13:54
add comment

Most computers use binary floating-point. Binary floating-point cannot represent 0.1 exactly (in binary, it's a recurring fraction). Thus, it also cannot represent 0.9 exactly either.

share|improve this answer
Almost posted the same thing...but if you read carefully you'll notice that the OP is expecting the rounding to happen in-place rather than having to assign the result to a new variable. –  Justin Niessner Jun 18 '11 at 2:35
@Justin: I agree; I upvoted adymitruk's answer too. :-) –  Chris Jester-Young Jun 18 '11 at 2:36
That's not true, though, in this case. –  minitech Jun 18 '11 at 2:36
@minitech: .NET doesn't "fix" anything. However, I'll grant that most floating-point display functions will display 16.9 because it rounds to, say, 6 decimal places, not 15. That doesn't negate my point that you cannot represent 16.9 exactly using only a double. –  Chris Jester-Young Jun 18 '11 at 2:41
@minitech just how would you expect .Net to know when the representation should be fixed and when it's accurate? The link you provide shows what the .NET framework does not how debuggers show the value (which was what Chris commented on) –  Rune FS Jun 18 '11 at 9:54
show 6 more comments

Double values are not precise by nature!

That happens because Double numbers are represented with binary digits, not decimal, so that the nearest representation of the requested number is given in the result.

share|improve this answer
add comment

Your Answer


