By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

Round

P: n/a
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
Nov 18 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 18 Nov 2008 15:13:16 -0800, shapper <md*****@gmail.comwrote:
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?
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
Nov 18 '08 #2

P: n/a
On Nov 18, 11:41*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 18 Nov 2008 15:13:16 -0800, shapper <mdmo...@gmail.comwrote:
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?

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.Coun t() /
(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

Nov 18 '08 #3

P: n/a
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
Nov 19 '08 #4

P: n/a
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
Nov 19 '08 #5

P: n/a
shapper <md*****@gmail.comwrote:
>
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.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.