3

I want to show number conversion double to int. But I am confused. Here is my codes.

private void tbxDouble_TextChanged(object sender, EventArgs e)
{
    tbxInt.Text = Convert.ToDouble(tbxDouble.Text).ToString("F0");
    tbxInt2.Text = Math.Round(Convert.ToDouble(tbxDouble.Text),0).ToString();
}

Results for 100,5 => 101 and 100

Results for 100,56 => 101 and 101

Results for 100,49 => 100 and 100

Why all results aren't 101? what is wrong with that?

lotomax
  • 113
  • 2
  • 12

3 Answers3

7

edit: As it was pointed in comments - my initial answer was wrong when I wrote that "F0" formatting will just truncate number. The correct answer is it will round but it will use other type of rounding called "Away from zero" rounding.

I assume cases with 100,49 and 100,56 are obvious, so let's take a look at 100,50

First of all let's take one step back: Convert.ToDouble(tbxDouble.Text) this line converted from string to double with value 100.50 and at this point you've done two different roundings.

First one:

value.ToString("F0");

is using Away from zero rounding

Second one:

Math.Round(value, 0)

is using Bankers Rounding

Bankers Rounding will round to the closest even number - so result will be 100. The Away From Zero rounding will round to 101 (101 is "more" away from zero than 100)

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Why is "F0" returning 101? Your answer doesn't seem to explain it. – InBetween Feb 17 '17 at 21:29
  • F0 just "cuts" decimal places - the results should be 100. there is no rounding here. Thanks for pointing out - I will edit my post to make it more clear – Michał Przybylak Feb 17 '17 at 21:30
  • But if it cuts decimal, then why doesn't it return `100`? – InBetween Feb 17 '17 at 21:31
  • 1
    This answer is wrong, how it's been upvoted 4 times is beyond me. "F0" does not truncate the number at all. It rounds the number as the other examples in the OP show. The issue here is that the rounding criteria doesn't seem to be the same as the one used by `Math.Round`. – InBetween Feb 17 '17 at 21:37
  • @InBetween yes, you are right - my mistake - F0 does not truncate, it will use "AwayFromZero" rounding. My mistake was because of line in documentation saying "Note that the precision specifier controls the number of digits in the string representation of a number. It does not round the number itself." but I've missed next part: "When precision specifier controls the number of fractional digits in the result string, the result strings reflect numbers that are rounded away from zero (that is, using MidpointRounding.AwayFromZero)." https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx – Michał Przybylak Feb 17 '17 at 21:44
0

if you want to round to the smallest integral value that is greater than or equal you have to use Math.Ceiling something like this

     class Program
{
    static void Main(string[] args)
    {
        double v2 = 100.56;
        double v3 = 100.49;
        Console.WriteLine(Math.Ceiling(v2));
        Console.WriteLine(Math.Ceiling(v3));

    }
}
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

I've found mistakes. We shouldn't use Math.Round, because sometimes results are wrong. We should use like below:

tbxInt3.Text = Math.Round(Convert.ToDouble(tbxDouble.Text), 0, MidpointRounding.AwayFromZero).ToString();

And here is the correct results:

Results for 100,5 => 101

Results for 100,56 => 101

Results for 100,49 => 100

More information link

Community
  • 1
  • 1
lotomax
  • 113
  • 2
  • 12