473,413 Members | 1,718 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,413 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 6798
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.