Rounding a number up

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

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

Thanks
 
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
 
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.
 
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/t...f=sr_1_1/002-7619090-8940005?v=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/t...f=sr_1_9/002-7619090-8940005?v=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 said:
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 said:
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
 
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 said:
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/t...f=sr_1_1/002-7619090-8940005?v=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/t...f=sr_1_9/002-7619090-8940005?v=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 said:
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 said:
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

:

Hello,

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

Thanks
 
Label myLabel = new Label();
double d = 41.8376;
d = Math.Round(d, 2, MidpointRounding.AwayFromZero);
myLabel.Text = d.ToString();


CsharpNewcommer said:
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 said:
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/t...f=sr_1_1/002-7619090-8940005?v=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/t...f=sr_1_9/002-7619090-8940005?v=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 said:
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.
:

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

:

Hello,

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

Thanks
 
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/d...uide/html/cpconcustomnumericformatstrings.asp

scott



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


CsharpNewcommer said:
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 said:
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/t...f=sr_1_1/002-7619090-8940005?v=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/t...f=sr_1_9/002-7619090-8940005?v=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



:

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.
:

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

:

Hello,

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

Thanks
 
That's perfect! Thanks for your help

D

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


CsharpNewcommer said:
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 said:
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/t...f=sr_1_1/002-7619090-8940005?v=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/t...f=sr_1_9/002-7619090-8940005?v=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



:

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.
:

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

:

Hello,

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

Thanks
 
Thanks Scott

Your information was very helpful.


D.


Scott Coonce said:
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/d...uide/html/cpconcustomnumericformatstrings.asp

scott



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


CsharpNewcommer said:
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.


:

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/t...f=sr_1_1/002-7619090-8940005?v=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/t...f=sr_1_9/002-7619090-8940005?v=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



:

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.
:

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

:

Hello,

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

Thanks
 
Back
Top