On Tue, 18 Nov 2008 15:51:35 -0800, shapper <md*****@gmail.comwrote:
I posted a simpler example to try to figure this out ... but my full
code is:
TagsPerFile = (float)Math.Round((double)(database.FilesTags.Coun t() /
(float)(database.Files.Count() == 0 ? 1 : database.Files.Count())), 2)
All due respect, that's hardly "full code". It's certainly more
complicated than what you posted before, but it's just one statement, and
not even one with the required semicolon.
TagsPerFile is a float ... however when I display the value it is not
rounded ...
How do you display the value if you can't get the code to compile?
Why do you say it's not rounded? What was the original value, and what is
the value returned by the statement that you earlier stated wouldn't even
compile?
Keep in mind that whether you use float or double, there are limits to
what numbers a floating point variable can represent. Rounding values can
only go so far, regardless of the floating point format you're using.
I used float because I don't need all the precision of the double ...
but should I use a double as a general type?
Either is fine. Unless this is a performance or memory bottleneck, it's
unlikely to make a difference. But, if you really need the _numeric_
value to be rounded exactly to two decimal places, you should probably be
using decimal, rather than float _or_ double.
Note that in most cases, one _doesn't_ need the numeric value to be
rounded _exactly_ to (for example) two decimal places. It's sufficient to
just use the closest representation possible for the data type, and use
numeric formatting if you need to display the value to the user or
otherwise represent it as text.
Pete