473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Round

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
5 6791
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matias Silva | last post by:
Can anybody tell me why I am getting rounding errors using the ROUND function. 3.7125 rounds to 3.70 when I use the following: TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2))) The correct...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
7
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545...
3
by: Krishna.K.1900 | last post by:
Does round() always perfectly return the output expected or are there some artifacts which don't allow perfect functionality Using python 2.5: 12.23 12.234 12.199999999999999 but was...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: josh logan | last post by:
Hello, I need a round function that _always_ rounds to the higher integer if the argument is equidistant between two integers. In Python 3.0, this is not the advertised behavior of the built-in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.