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

Z

Zeng

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!
 
S

Samuel R. Neff

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
 
Z

Zeng

It's an inconsistency type bug. There is no reason for the same framework to
round numbers differently.
 
R

RobinS

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

Samuel R. Neff

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
 
Z

Zeng

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

Samuel R. Neff

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top