473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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((dou ble)c,2)

What am I doing wrong?

Thanks,
Miguel
Nov 18 '08 #1
5 6828
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((dou ble)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...@nn owslpianmk.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((dou ble)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.Rou nd((double)(dat abase.FilesTags .Count() /
(float)(databas e.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.Rou nd((double)(dat abase.FilesTags .Count() /
(float)(databas e.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.Rou nd((double)
(database.Files Tags.Count() / (float)(databas e.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.Rou nd((double)
(database.File sTags.Count() / (float)(databas e.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
3131
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 value should be 3.71 I could round to the 3rd decimal place ROUND(X,3) and that would round it correctly to 3.71 but that would mean I would have to change the ROUND function in another
6
18672
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 and will convert between date/time and double automatically. The double value Access (or VB) creates is based on 1 day = 1.0 and the fractional part represents a
17
5659
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 x=3.24 , result is is false
9
7385
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 different. Programming Microsoft Windows with Microsoft Visual Basic.NET "The Round method with a single argument return the whole number nearest to the argument. If the argument to Round is midway between two whole numbers,
4
7254
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 the value to one decimal place with a zero EG:
10
16036
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) 0.74
7
4476
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 -".number_format(20.545, 2, ',', '.')."<br>"; echo "number_format 20.555 -".number_format(20.555, 2, ',', '.')."<br>"; PHP Version 4.3.0 / FreeBSD
3
1834
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 expecting 12.2
4
10900
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
6558
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 function round() as seen below: 0 2 2
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.