Round

S

shapper

Hello,

I have the following:
int a; int b; var c

c = a / (float)b;

I used float to not get an int value as result. I think in C# I must.

But know I want to round c to something like 3.48 or 7.50, ...

I am using Math.Round(c,2) but I get the error:
Cannot implicitly convert type 'double' to 'float'. An explicit
conversion exists (are you missing a cast?)

I also tried Math.Round((double)c,2)

What am I doing wrong?

Thanks,
Miguel
 
S

shapper

By assigning an expression of type "float" to "c", the type of "c" winds  
up being "float.  Presumably you are trying to assign the result of  
Math.Round() to "c" but you can't implicitly convert the type 'double'  
(which is what Math.Round() is returning) to 'float'.  But, there is an 
explicit cast that you can use.

Did you forget to include it?

Pete

I posted a simpler example to try to figure this out ... but my full
code is:
TagsPerFile = (float)Math.Round((double)(database.FilesTags.Count() /
(float)(database.Files.Count() == 0 ? 1 : database.Files.Count())), 2)

TagsPerFile is a float ... however when I display the value it is not
rounded ...

I used float because I don't need all the precision of the double ...
but should I use a double as a general type?

Thanks,
Miguel
 
S

shapper

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.

Yes, I know. Sometimes I tend to write a simplification because I am
trying to understand something more than trying people to solve the
problem.

Then if I am not able to solve I post the entire code ... usually I am
able to solve it.

Grrr, it is working! I was trying in a earlier build version and
didn't notice!

fileStat = new FileStat {
FileCount = database.Files.Count(),
TagsPerFile = (float)Math.Round((double)
(database.FilesTags.Count() / (float)(database.Files.Count() == 0 ?
1 : database.Files.Count())), 2)
};

I didn't post the other lines since they were not relevant for the
problem.
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.

I was using this table:
http://msdn.microsoft.com/en-us/library/cs7y5x0x.aspx

I didn't know I could use decimal ...

Thank You,
Miguel
 
T

Tim Roberts

shapper said:
Grrr, it is working! I was trying in a earlier build version and
didn't notice!

fileStat = new FileStat {
FileCount = database.Files.Count(),
TagsPerFile = (float)Math.Round((double)
(database.FilesTags.Count() / (float)(database.Files.Count() == 0 ?
1 : database.Files.Count())), 2)
};

OK, but what's the POINT of this? If the only reason you are doing this is
so that TagsPerFile prints out with 2 digits, then the CORRECT solution is
to kepp all of the original precision, and specify 2 digits in the
formatting when you actually display it.

Remember that 2.70 (for example) cannot be represented exactly in a binary
floating point number, whether you use single, double, or ten-tuple
precision. It's all an approximation. When you start with 2.699998 and
round it to 2 places, you are probably ending up with the same value.
They're both approximations. The key is to DISPLAY it in a way that gives
you what you need to know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top