Click here to Skip to main content
15,884,298 members

C# Math.Round(double,Int32) Troubles

Electron John asked:

Open original thread
I am using Visual C# 2010 Express and am stumped with the Math.Round method.

Two overloads are important to the context of my question:
1- Math.Round(decimal d, int n)
2- Math.Round(double d, int n)

I am trying to round to 5 total base-10 digits. See code snippet below:

C#
int fractional_digits = 4;
double number_to_round = 123.456789;
double rounded_result = Math.Round(number_to_round, fractional_digits);
number_to_round = 11223344.556677;
rounded_result = Math.Round(number_to_round, fractional_digits);
number_to_round = 876.54321E+57;
rounded_result = Math.Round(number_to_round, fractional_digits);


The code is redundant to show the issue three times. The first rounded result gives 123.4568 not 123.46 (or 1.2346E+2) (1 integer + 4 fractional digits).

The second rounded result is 11223344.5567 instead of 11223000 (or 1.1223E+7).
Lastly, the rounded result is 8.7654321E+59 instead of 8.7654E+59.

I need only 5 significant digits. The code example seems to be using method 1 as shown above - not method 2.

I need method 2. (See comment header copied below).
C#
//
// Summary:
//     Rounds a double-precision floating-point value to a specified number of fractional
//     digits.
//
// Parameters:
//   value:
//     A double-precision floating-point number to be rounded.
//
//   digits:
//     The number of fractional digits in the return value.
//
// Returns:
//     The number nearest to value that contains a number of fractional digits equal
//     to digits.
//
// Exceptions:
//   System.ArgumentOutOfRangeException:
//     digits is less than 0 or greater than 15.
public static double Round(double value, int digits);


How do I force the compiler to use method 2? Any other suggestions?

I have done a basic search for help on this site (via Google) but all examples only show a single place-value in the integer position.

Thanks in advance.

-EJ
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900