473,404 Members | 2,114 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,404 software developers and data experts.

Rounding a number up

Hello,

In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
double.

Thanks
Nov 17 '05 #1
8 1966
Hi,
if it was me I would do something like:

double d = 41.8376;

Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));

or

Console.WriteLine((int)((d + 0.005) * 100) / 100.0);

Hope that helps
Mark R Dawson
http://www.markdawson.org

Mark

"CsharpNewcommer" wrote:
Hello,

In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
double.

Thanks

Nov 17 '05 #2
Hi Mark,

Thank you for the response. In this case is "d" a variable name? So how
would this work when displaying "d" to an object (such as a label ) on a
form or writing it to a database?
eg.) this writting it to the database: drNewRow["fldSSTax"] = lblSSTax.Text;
and this is displaying it to a label: lblSSTax.Text = SSTax.ToString();

Also can you suggest a really good C# reference book that covers more than
"Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!

Thanks D.
"Mark R. Dawson" wrote:
Hi,
if it was me I would do something like:

double d = 41.8376;

Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));

or

Console.WriteLine((int)((d + 0.005) * 100) / 100.0);

Hope that helps
Mark R Dawson
http://www.markdawson.org

Mark

"CsharpNewcommer" wrote:
Hello,

In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
double.

Thanks

Nov 17 '05 #3
Hi,
d in the example was a variable i.e

double d = 12.123;

If I want to write this double value to a label then I need to get the
text representation of the number and assign it to the text property of a
label i.e.

Label myLabel = new Label();
myLabel.Text = d.ToString();

I am not sure of any good beginners guides to C#, I do not own any so I
can't really recommend one, I do have some more advanced books which I liked
and can recommend but these assume some level of knowledge in C#:

Professional C#
http://www.amazon.com/exec/obidos/tg...glance&s=books
Pro C# 2005 - this is a really good book but expects you to have a good
understanding of C#
http://www.amazon.com/exec/obidos/tg...glance&s=books

I believe there are beginner versions of these books as well which you may
find useful.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"CsharpNewcommer" wrote:
Hi Mark,

Thank you for the response. In this case is "d" a variable name? So how
would this work when displaying "d" to an object (such as a label ) on a
form or writing it to a database?
eg.) this writting it to the database: drNewRow["fldSSTax"] = lblSSTax.Text;
and this is displaying it to a label: lblSSTax.Text = SSTax.ToString();

Also can you suggest a really good C# reference book that covers more than
"Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!

Thanks D.
"Mark R. Dawson" wrote:
Hi,
if it was me I would do something like:

double d = 41.8376;

Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));

or

Console.WriteLine((int)((d + 0.005) * 100) / 100.0);

Hope that helps
Mark R Dawson
http://www.markdawson.org

Mark

"CsharpNewcommer" wrote:
Hello,

In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
double.

Thanks

Nov 17 '05 #4
Thanks Mark,

You've been very helpful. Thanks for the information. I have just one more
question if I may.

Now how would I incorporate the first bits of info you gave,
ie. Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero)); or
Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
with, ie. myLabel.Text = d.ToString(); ? Or can I not do this? To be clear,
How do I use the "Math.Round(d, 2, MidpointRounding.AwayFromZero)); with a
myLabel.Text = d.ToString(); ?

Yes, I am a beginner with C# but I have programmed in other languages so I
might be able to comprehend to books you suggested. I just need a really good
reference book that covers most if not EVERYTHING. I know that's too much to
ask for!

Thanks
D.
"Mark R. Dawson" wrote:
Hi,
d in the example was a variable i.e

double d = 12.123;

If I want to write this double value to a label then I need to get the
text representation of the number and assign it to the text property of a
label i.e.

Label myLabel = new Label();
myLabel.Text = d.ToString();

I am not sure of any good beginners guides to C#, I do not own any so I
can't really recommend one, I do have some more advanced books which I liked
and can recommend but these assume some level of knowledge in C#:

Professional C#
http://www.amazon.com/exec/obidos/tg...glance&s=books
Pro C# 2005 - this is a really good book but expects you to have a good
understanding of C#
http://www.amazon.com/exec/obidos/tg...glance&s=books

I believe there are beginner versions of these books as well which you may
find useful.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"CsharpNewcommer" wrote:
Hi Mark,

Thank you for the response. In this case is "d" a variable name? So how
would this work when displaying "d" to an object (such as a label ) on a
form or writing it to a database?
eg.) this writting it to the database: drNewRow["fldSSTax"] = lblSSTax.Text;
and this is displaying it to a label: lblSSTax.Text = SSTax.ToString();

Also can you suggest a really good C# reference book that covers more than
"Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!

Thanks D.
"Mark R. Dawson" wrote:
Hi,
if it was me I would do something like:

double d = 41.8376;

Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));

or

Console.WriteLine((int)((d + 0.005) * 100) / 100.0);

Hope that helps
Mark R Dawson
http://www.markdawson.org

Mark

"CsharpNewcommer" wrote:

> Hello,
>
> In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
> double.
>
> Thanks

Nov 17 '05 #5
Label myLabel = new Label();
double d = 41.8376;
d = Math.Round(d, 2, MidpointRounding.AwayFromZero);
myLabel.Text = d.ToString();
"CsharpNewcommer" wrote:
Thanks Mark,

You've been very helpful. Thanks for the information. I have just one more
question if I may.

Now how would I incorporate the first bits of info you gave,
ie. Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero)); or
Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
with, ie. myLabel.Text = d.ToString(); ? Or can I not do this? To be clear,
How do I use the "Math.Round(d, 2, MidpointRounding.AwayFromZero)); with a
myLabel.Text = d.ToString(); ?

Yes, I am a beginner with C# but I have programmed in other languages so I
might be able to comprehend to books you suggested. I just need a really good
reference book that covers most if not EVERYTHING. I know that's too much to
ask for!

Thanks
D.
"Mark R. Dawson" wrote:
Hi,
d in the example was a variable i.e

double d = 12.123;

If I want to write this double value to a label then I need to get the
text representation of the number and assign it to the text property of a
label i.e.

Label myLabel = new Label();
myLabel.Text = d.ToString();

I am not sure of any good beginners guides to C#, I do not own any so I
can't really recommend one, I do have some more advanced books which I liked
and can recommend but these assume some level of knowledge in C#:

Professional C#
http://www.amazon.com/exec/obidos/tg...glance&s=books
Pro C# 2005 - this is a really good book but expects you to have a good
understanding of C#
http://www.amazon.com/exec/obidos/tg...glance&s=books

I believe there are beginner versions of these books as well which you may
find useful.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"CsharpNewcommer" wrote:
Hi Mark,

Thank you for the response. In this case is "d" a variable name? So how
would this work when displaying "d" to an object (such as a label ) on a
form or writing it to a database?
eg.) this writting it to the database: drNewRow["fldSSTax"] = lblSSTax.Text;
and this is displaying it to a label: lblSSTax.Text = SSTax.ToString();

Also can you suggest a really good C# reference book that covers more than
"Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!

Thanks D.
"Mark R. Dawson" wrote:

> Hi,
> if it was me I would do something like:
>
> double d = 41.8376;
>
> Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));
>
> or
>
> Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
>
> Hope that helps
> Mark R Dawson
> http://www.markdawson.org
>
>
>
> Mark
>
> "CsharpNewcommer" wrote:
>
> > Hello,
> >
> > In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
> > double.
> >
> > Thanks

Nov 17 '05 #6
I think it would be only fair to mention NumberFormatting...

such as myLabel.Text = d.ToString("#.00");

that way, the value of "d" doesn't change--only the text representation of
it in the label.

for more info on the numeric formatting, see
http://msdn.microsoft.com/library/de...matstrings.asp

scott

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Label myLabel = new Label();
double d = 41.8376;
d = Math.Round(d, 2, MidpointRounding.AwayFromZero);
myLabel.Text = d.ToString();
"CsharpNewcommer" wrote:
Thanks Mark,

You've been very helpful. Thanks for the information. I have just one
more
question if I may.

Now how would I incorporate the first bits of info you gave,
ie. Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));
or
Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
with, ie. myLabel.Text = d.ToString(); ? Or can I not do this? To be
clear,
How do I use the "Math.Round(d, 2, MidpointRounding.AwayFromZero)); with
a
myLabel.Text = d.ToString(); ?

Yes, I am a beginner with C# but I have programmed in other languages so
I
might be able to comprehend to books you suggested. I just need a really
good
reference book that covers most if not EVERYTHING. I know that's too much
to
ask for!

Thanks
D.
"Mark R. Dawson" wrote:
> Hi,
> d in the example was a variable i.e
>
> double d = 12.123;
>
> If I want to write this double value to a label then I need to get
> the
> text representation of the number and assign it to the text property of
> a
> label i.e.
>
> Label myLabel = new Label();
> myLabel.Text = d.ToString();
>
> I am not sure of any good beginners guides to C#, I do not own any so
> I
> can't really recommend one, I do have some more advanced books which I
> liked
> and can recommend but these assume some level of knowledge in C#:
>
> Professional C#
> http://www.amazon.com/exec/obidos/tg...glance&s=books
>
>
> Pro C# 2005 - this is a really good book but expects you to have a good
> understanding of C#
> http://www.amazon.com/exec/obidos/tg...glance&s=books
>
> I believe there are beginner versions of these books as well which you
> may
> find useful.
>
> Hope that helps
> Mark R Dawson
> http://www.markdawson.org
>
>
>
> "CsharpNewcommer" wrote:
>
> > Hi Mark,
> >
> > Thank you for the response. In this case is "d" a variable name? So
> > how
> > would this work when displaying "d" to an object (such as a label )
> > on a
> > form or writing it to a database?
> > eg.) this writting it to the database: drNewRow["fldSSTax"] =
> > lblSSTax.Text;
> > and this is displaying it to a label: lblSSTax.Text =
> > SSTax.ToString();
> >
> > Also can you suggest a really good C# reference book that covers more
> > than
> > "Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!
> >
> > Thanks D.
> > "Mark R. Dawson" wrote:
> >
> > > Hi,
> > > if it was me I would do something like:
> > >
> > > double d = 41.8376;
> > >
> > > Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));
> > >
> > > or
> > >
> > > Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
> > >
> > > Hope that helps
> > > Mark R Dawson
> > > http://www.markdawson.org
> > >
> > >
> > >
> > > Mark
> > >
> > > "CsharpNewcommer" wrote:
> > >
> > > > Hello,
> > > >
> > > > In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are
> > > > casted as
> > > > double.
> > > >
> > > > Thanks

Nov 17 '05 #7
That's perfect! Thanks for your help

D

"Mark R. Dawson" wrote:
Label myLabel = new Label();
double d = 41.8376;
d = Math.Round(d, 2, MidpointRounding.AwayFromZero);
myLabel.Text = d.ToString();
"CsharpNewcommer" wrote:
Thanks Mark,

You've been very helpful. Thanks for the information. I have just one more
question if I may.

Now how would I incorporate the first bits of info you gave,
ie. Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero)); or
Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
with, ie. myLabel.Text = d.ToString(); ? Or can I not do this? To be clear,
How do I use the "Math.Round(d, 2, MidpointRounding.AwayFromZero)); with a
myLabel.Text = d.ToString(); ?

Yes, I am a beginner with C# but I have programmed in other languages so I
might be able to comprehend to books you suggested. I just need a really good
reference book that covers most if not EVERYTHING. I know that's too much to
ask for!

Thanks
D.
"Mark R. Dawson" wrote:
Hi,
d in the example was a variable i.e

double d = 12.123;

If I want to write this double value to a label then I need to get the
text representation of the number and assign it to the text property of a
label i.e.

Label myLabel = new Label();
myLabel.Text = d.ToString();

I am not sure of any good beginners guides to C#, I do not own any so I
can't really recommend one, I do have some more advanced books which I liked
and can recommend but these assume some level of knowledge in C#:

Professional C#
http://www.amazon.com/exec/obidos/tg...glance&s=books
Pro C# 2005 - this is a really good book but expects you to have a good
understanding of C#
http://www.amazon.com/exec/obidos/tg...glance&s=books

I believe there are beginner versions of these books as well which you may
find useful.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"CsharpNewcommer" wrote:

> Hi Mark,
>
> Thank you for the response. In this case is "d" a variable name? So how
> would this work when displaying "d" to an object (such as a label ) on a
> form or writing it to a database?
> eg.) this writting it to the database: drNewRow["fldSSTax"] = lblSSTax.Text;
> and this is displaying it to a label: lblSSTax.Text = SSTax.ToString();
>
> Also can you suggest a really good C# reference book that covers more than
> "Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!
>
> Thanks D.
> "Mark R. Dawson" wrote:
>
> > Hi,
> > if it was me I would do something like:
> >
> > double d = 41.8376;
> >
> > Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));
> >
> > or
> >
> > Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
> >
> > Hope that helps
> > Mark R Dawson
> > http://www.markdawson.org
> >
> >
> >
> > Mark
> >
> > "CsharpNewcommer" wrote:
> >
> > > Hello,
> > >
> > > In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are casted as
> > > double.
> > >
> > > Thanks

Nov 17 '05 #8
Thanks Scott

Your information was very helpful.
D.
"Scott Coonce" wrote:
I think it would be only fair to mention NumberFormatting...

such as myLabel.Text = d.ToString("#.00");

that way, the value of "d" doesn't change--only the text representation of
it in the label.

for more info on the numeric formatting, see
http://msdn.microsoft.com/library/de...matstrings.asp

scott

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Label myLabel = new Label();
double d = 41.8376;
d = Math.Round(d, 2, MidpointRounding.AwayFromZero);
myLabel.Text = d.ToString();
"CsharpNewcommer" wrote:
Thanks Mark,

You've been very helpful. Thanks for the information. I have just one
more
question if I may.

Now how would I incorporate the first bits of info you gave,
ie. Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));
or
Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
with, ie. myLabel.Text = d.ToString(); ? Or can I not do this? To be
clear,
How do I use the "Math.Round(d, 2, MidpointRounding.AwayFromZero)); with
a
myLabel.Text = d.ToString(); ?

Yes, I am a beginner with C# but I have programmed in other languages so
I
might be able to comprehend to books you suggested. I just need a really
good
reference book that covers most if not EVERYTHING. I know that's too much
to
ask for!

Thanks
D.
"Mark R. Dawson" wrote:

> Hi,
> d in the example was a variable i.e
>
> double d = 12.123;
>
> If I want to write this double value to a label then I need to get
> the
> text representation of the number and assign it to the text property of
> a
> label i.e.
>
> Label myLabel = new Label();
> myLabel.Text = d.ToString();
>
> I am not sure of any good beginners guides to C#, I do not own any so
> I
> can't really recommend one, I do have some more advanced books which I
> liked
> and can recommend but these assume some level of knowledge in C#:
>
> Professional C#
> http://www.amazon.com/exec/obidos/tg...glance&s=books
>
>
> Pro C# 2005 - this is a really good book but expects you to have a good
> understanding of C#
> http://www.amazon.com/exec/obidos/tg...glance&s=books
>
> I believe there are beginner versions of these books as well which you
> may
> find useful.
>
> Hope that helps
> Mark R Dawson
> http://www.markdawson.org
>
>
>
> "CsharpNewcommer" wrote:
>
> > Hi Mark,
> >
> > Thank you for the response. In this case is "d" a variable name? So
> > how
> > would this work when displaying "d" to an object (such as a label )
> > on a
> > form or writing it to a database?
> > eg.) this writting it to the database: drNewRow["fldSSTax"] =
> > lblSSTax.Text;
> > and this is displaying it to a label: lblSSTax.Text =
> > SSTax.ToString();
> >
> > Also can you suggest a really good C# reference book that covers more
> > than
> > "Teach Yourself Microsoft C#. Net 2003 in 24 Hours"!
> >
> > Thanks D.
> > "Mark R. Dawson" wrote:
> >
> > > Hi,
> > > if it was me I would do something like:
> > >
> > > double d = 41.8376;
> > >
> > > Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));
> > >
> > > or
> > >
> > > Console.WriteLine((int)((d + 0.005) * 100) / 100.0);
> > >
> > > Hope that helps
> > > Mark R Dawson
> > > http://www.markdawson.org
> > >
> > >
> > >
> > > Mark
> > >
> > > "CsharpNewcommer" wrote:
> > >
> > > > Hello,
> > > >
> > > > In C#, how can I roundoff 41.8376 to 41.84? Oh these fields are
> > > > casted as
> > > > double.
> > > >
> > > > Thanks


Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
248
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.