469,898 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Discrepancy between Math.Round and ToString( "F" ) method

Math.Round has good behavior as following:

Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7' is odd
However, if format.NumberDecimalDigits is 1
decimal d = 3.45M;
d.ToString( "F", format ); //Return 3.5 - this is different from Math.Round;
is this a bug?

d= 3.75M
d.ToString( "F", format) ; //Returns 3.8 - this is the same as Math.Round

Could someone know if I missed anything or I have to do the rounding myself
before using ToString( "F" )?

thanks!
Mar 2 '07 #1
6 8903

Not a bug. Math.Round does bankers roundings and ToString does normal
arithmetic rounding.

Been discussed at length on the groups and there are MS KB articles
about it so search if you want more details.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 2 Mar 2007 12:14:46 -0800, "Zeng" <ze**@nononospam.comwrote:
>Math.Round has good behavior as following:

Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7' is odd
However, if format.NumberDecimalDigits is 1
decimal d = 3.45M;
d.ToString( "F", format ); //Return 3.5 - this is different from Math.Round;
is this a bug?

d= 3.75M
d.ToString( "F", format) ; //Returns 3.8 - this is the same as Math.Round

Could someone know if I missed anything or I have to do the rounding myself
before using ToString( "F" )?

thanks!
Mar 2 '07 #2
It's an inconsistency type bug. There is no reason for the same framework to
round numbers differently.
"Samuel R. Neff" <sa********@nomail.comwrote in message
news:hg********************************@4ax.com...
>
Not a bug. Math.Round does bankers roundings and ToString does normal
arithmetic rounding.

Been discussed at length on the groups and there are MS KB articles
about it so search if you want more details.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 2 Mar 2007 12:14:46 -0800, "Zeng" <ze**@nononospam.comwrote:
>>Math.Round has good behavior as following:

Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4
is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7' is
odd
However, if format.NumberDecimalDigits is 1
decimal d = 3.45M;
d.ToString( "F", format ); //Return 3.5 - this is different from
Math.Round;
is this a bug?

d= 3.75M
d.ToString( "F", format) ; //Returns 3.8 - this is the same as Math.Round

Could someone know if I missed anything or I have to do the rounding
myself
before using ToString( "F" )?

thanks!

Mar 3 '07 #3
Sure there is. Some people use bankers rounding, and this is the
traditional way in .Net. Some people use regular rounding. (I think *most*
people use regular rounding, but I could be wrong.)

Robin S.
-------------------------
"Zeng" <ze**@nononospam.comwrote in message
news:um**************@TK2MSFTNGP06.phx.gbl...
It's an inconsistency type bug. There is no reason for the same framework
to round numbers differently.
"Samuel R. Neff" <sa********@nomail.comwrote in message
news:hg********************************@4ax.com...
>>
Not a bug. Math.Round does bankers roundings and ToString does normal
arithmetic rounding.

Been discussed at length on the groups and there are MS KB articles
about it so search if you want more details.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 2 Mar 2007 12:14:46 -0800, "Zeng" <ze**@nononospam.comwrote:
>>>Math.Round has good behavior as following:

Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because
4 is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7' is
odd
However, if format.NumberDecimalDigits is 1
decimal d = 3.45M;
d.ToString( "F", format ); //Return 3.5 - this is different from
Math.Round;
is this a bug?

d= 3.75M
d.ToString( "F", format) ; //Returns 3.8 - this is the same as
Math.Round

Could someone know if I missed anything or I have to do the rounding
myself
before using ToString( "F" )?

thanks!


Mar 4 '07 #4

While I don't like the behavior, it works as it's documented. If it
does what it's documented to do then it's certainly not a bug.

I personally prefer Java's new rounding functionality which gives you
a choice between 5-7 different rounding methodologies.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 2 Mar 2007 16:15:41 -0800, "Zeng" <ze**@nononospam.comwrote:
>It's an inconsistency type bug. There is no reason for the same framework to
round numbers differently.

Mar 5 '07 #5
That's such low standard for a bug definition. Documenting a bug doesn't
mean the bug would go away. Documentation is typically done after the code
is completed and signed off by development team.
"Samuel R. Neff" <sa********@nomail.comwrote in message
news:vr********************************@4ax.com...
>
While I don't like the behavior, it works as it's documented. If it
does what it's documented to do then it's certainly not a bug.

I personally prefer Java's new rounding functionality which gives you
a choice between 5-7 different rounding methodologies.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 2 Mar 2007 16:15:41 -0800, "Zeng" <ze**@nononospam.comwrote:
>>It's an inconsistency type bug. There is no reason for the same framework
to
round numbers differently.


Mar 5 '07 #6

It's not a bug. It's programmed to work that way which is confirmed
by reading the documentation. There is a ton of history on this issue
in the forums and online. Search.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Mon, 5 Mar 2007 08:05:02 -0800, "Zeng" <ze**@nononospam.comwrote:
>That's such low standard for a bug definition. Documenting a bug doesn't
mean the bug would go away. Documentation is typically done after the code
is completed and signed off by development team.
Mar 6 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jef Driesen | last post: by
1 post views Thread by Steven Spits | last post: by
4 posts views Thread by Larry Smith | last post: by
1 post views Thread by Marc Gravell | last post: by
2 posts views Thread by maminx | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.