-1

I use a numericupdown set min = 0 maximum = 59 and increment = 1 When i check the debugger at

Private Sub Numericsec_ValueChanged(sender As Object, e As System.EventArgs) Handles Numericsec.ValueChanged

    unitsec = Convert.ToInt32(DirectCast(sender, NumericUpDown).Value Mod 10)
    tensec = Convert.ToInt32(DirectCast(sender, NumericUpDown).Value / 10)

when value is 5 then
unitsec = 5
tensec = 0
but when value is 6
unitsec = 5
tensec = 1 ????

Thanks

djv
  • 15,168
  • 7
  • 48
  • 72
ShayW
  • 19
  • 4
  • 2
    And if you write `Dim tensec = Convert.ToInt32(6 / 10)`, what's the result? If you instead write `Dim tensec = Convert.ToInt32(6 \ 10)`? (Don't blame poor NumericUpDown) – Jimi Aug 20 '19 at 20:09
  • 1
    use `\ 10` for integer division, and no need for `Convert.ToInt32` – Slai Aug 20 '19 at 20:10
  • @Slai true, but NumericUpDown.Value is `Decimal` and you will need some sort of conversion anyway because \ takes two `Long` – djv Aug 20 '19 at 20:51
  • `but when value is 6 ... unitsec = 5` I don't think so. Check again. It is basically `Convert.ToInt32(6D Mod 10)` and there is no way that's 5 – djv Aug 20 '19 at 20:56
  • tensec = Convert.ToInt32(6 \ 10) = 1 – ShayW Aug 20 '19 at 20:59
  • @ShayW I suggest you try it. From [Integer Division](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/integer-division-operator), *Result: The result is the integer quotient of expression1 divided by expression2, which discards any remainder and retains only the integer portion. This is known as truncation.* Plus you can't just put 6 in there if you expect to use NumericUpDown.Value, since that is Decimal and the \ operator won't accept it. I'm struggling to understand how you keep posting incorrect arithmetic results... `6 \ 10 = 0` – djv Aug 20 '19 at 21:04
  • tensec = Convert.ToInt32(Math.Truncate(DirectCast(sender, NumericUpDown).Value / 10)) it works – ShayW Aug 20 '19 at 21:12
  • @ShayW I think I see the source of confusion. See [this Answer](https://stackoverflow.com/a/11431856/832052) wrt how midpoint rounding is handled by Convert.ToInt32. Also see my answer. – djv Aug 20 '19 at 21:39

1 Answers1

0

There's nothing special about NumericUpDown.Value; it's just a Decimal. Test this code and confirm that you get the same results. It might just be an issue with understanding the various arithmetic and conversions involved.

Test code:

Console.WriteLine($"Convert.ToInt32(5D Mod 10): {Convert.ToInt32(5D Mod 10)}")
Console.WriteLine($"Convert.ToInt32(5D / 10): {Convert.ToInt32(5D / 10)}")
Console.WriteLine($"Convert.ToInt32(6D Mod 10): {Convert.ToInt32(6D Mod 10)}")
Console.WriteLine($"Convert.ToInt32(6D / 10): {Convert.ToInt32(6D / 10)}")
Console.WriteLine($"Convert.ToInt32(6D) \ 10: {Convert.ToInt32(6D) \ 10}")
Console.WriteLine($"Convert.ToInt32(5D) \ 10: {Convert.ToInt32(5D) \ 10}")

Output:

Convert.ToInt32(5D Mod 10): 5
Convert.ToInt32(5D / 10): 0
Convert.ToInt32(6D Mod 10): 6
Convert.ToInt32(6D / 10): 1
Convert.ToInt32(6D) \ 10: 0
Convert.ToInt32(5D) \ 10: 0

Explanation:
5D Mod 10: No secret here. 5 Mod 10 is 5. Conversion does nothing
5D / 10: Convert.ToInt32 uses banker's rounding i.e. midpoint rounding goes to the nearest even number
6D Mod 10: Again, no secret here
6D / 10: Here it might be confusing because now you have 0.6 and it is rounded to the nearest integer. Midpoint rounding down to 0 doesn't apply because it is > the midpoint.
5D \ 10, 6D \ 10: Integer division discards decimals. Both result in 0.

djv
  • 15,168
  • 7
  • 48
  • 72