problem with the Mod operator

EPYK

Member
Joined
Jan 14, 2007
Messages
12
Programming Experience
1-3
i have a problem with the mod operator.
example:
1001.01 mod 1000 returns 1.00999999999909

which will not work for me..

it should return 1.01

any help with this
 
You can round it with Math.Round(double, digits)

This also works:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] res [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = 1001.01D [/SIZE][SIZE=2][COLOR=#0000ff]Mod[/COLOR][/SIZE][SIZE=2] 1000 'res = 1.01[/SIZE]
More about this here: Troubleshooting Data Types where they mention Mod operator and floating-point numbers and Doubles.
 
heres what i got in the last hour

heres what i pulled outta my butt...
VB.NET:
 Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim strMoney As String
        Dim dblMoney As Double
        Dim dblTotal As Double
        Dim arrDivisor() As Double = {1000.0, 100.0, 50.0, 20.0, 10.0, 5.0, 1.0, 0.25, 0.1, 0.05, 0.01}
        Dim arrChangeType() As String = {"thousand dollar bills: ", "hundred dollar bills: ", "fifty dollar bills: ", "twenty dollar bills: ", "ten dollar bills: ", "five dollar bills: ", "one dollar bills: ", "quarters: ", "dimes: ", "nickels: ", "pennies: "}
        Dim arrChangeAmounts(11) As Integer


        Dim intCounter As Integer

        strMoney = txtMoney.Text

        If StringCheck(strMoney, "cur") = True Then
            MsgBox("You have entered a bad numeric amount.", , "ERROR")
            txtMoney.Clear()
        Else

            dblMoney = CDbl(strMoney)

            For intCounter = 0 To 10   ' Set up 11 repetitions.

                If dblMoney > arrDivisor(intCounter) Or dblMoney = arrDivisor(intCounter) Then

                    'division, result is a double. I.E. 1.234  gets converted to integer and drops the decimal portion
                    arrChangeAmounts(intCounter) = CInt(dblMoney / arrDivisor(intCounter))

                    'updates the label array with a bill/change type and ammount of that type
                    LabelArray(intCounter + 1).Text = arrChangeType(intCounter) & arrChangeAmounts(intCounter)

                    'the adds all the change made and puts it in a disabled textbox
                    dblTotal = dblTotal + (arrChangeAmounts(intCounter) * arrDivisor(intCounter))

                    ' i made my own modulus function to get over the improper division
                    dblMoney = ModNum(dblMoney, arrDivisor(intCounter))

                    ' this tries to fix the problem with MS's version of division 
                    dblMoney = dblMoney + 0.00001

                Else
                    LabelArray(intCounter + 1).Text = arrChangeType(intCounter) & "0"
                End If

            Next intCounter   ' Increment intCounter.

            txtTotal.Text = CStr(dblTotal)

        End If

    End Sub
with my own modulus function
VB.NET:
    Private Function ModNum(ByVal dblNumber1 As Double, ByVal dblNumber2 As Double) As Double
        Dim dblRemainder As Double

        dblRemainder = dblNumber1 / dblNumber2

        While dblRemainder > 1
            dblRemainder -= 1
        End While

        dblRemainder = dblRemainder * dblNumber2

        '1001.01 / 1000 = 1.00101
        '1.00101 - 1 = 0.00101
        '0.00101 * 1000 = 1.01

        Return dblRemainder
    End Function
 
my program takes an amount of money and divides it up into the most efficient denomination of bills...

main algorithm:
loop
bill amount = integer(money / particular bill)
money = money MOD particular bill
end loop

but MS messed it all up
 
Back
Top