Round numbers.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Round numbers.

  1. #1
    John K. Guest

    Round numbers.

    Hi!

    Let's say I make up my own numeric system.
    Instead of 1,2,3,4,5,6........and so on,
    numbers can only be 1,5,10,15,20......and so on.

    Then let's say I create a random number, which I
    want to convert into my system.
    Let's say I get the number 24.
    Now, I don't actually know the number is 24.
    How do I round that number into my system?
    How should my computer know that it should
    be turned into 25 instead? Or that 32 should be 30??

    Thanks!



  2. #2
    Shawn K. Hall Guest

    Re: Round numbers.

    John,

    I think the best way to implement this would be to divide the new
    value by the imcrement you've chosen (in this case 5) and then
    multiply it by the value again. These should either be two seperate
    operations with a long value or you will need to convert the value
    after the div to a whole number.

    Example:
    ' takes a number and rounds it to the nearest base 5 number
    Function Make5(ByVal lNum&)
    lNum = lNum / 5 'automatically rounds because lNum is a long
    Make5 = lNum * 5 'sets the new base value
    End Function

    ?Make5(24) 'returns 25
    ?Make5(22) 'returns 20

    Regards,
    --
    Shawn K. Hall
    Programmer / Analyst
    *Please* post/respond in the newsgroups!
    http://i.am/shawnkhall


    "John K." <john.k@home.se> wrote in message
    news:392ee216@news.devx.com...
    > Hi!
    >
    > Let's say I make up my own numeric system.
    > Instead of 1,2,3,4,5,6........and so on,
    > numbers can only be 1,5,10,15,20......and so on.
    >
    > Then let's say I create a random number, which I
    > want to convert into my system.
    > Let's say I get the number 24.
    > Now, I don't actually know the number is 24.
    > How do I round that number into my system?
    > How should my computer know that it should
    > be turned into 25 instead? Or that 32 should be 30??
    >
    > Thanks!
    >
    >




  3. #3
    Rick Rothstein Guest

    Re: Round numbers.

    First, I'd like to point out that your desired number system should be 0, 5,
    10, 15, etc. instead of 1, 5, etc., otherwise your 1st interval will be
    different from all the rest. Now, there are two ways to handle what you
    want.

    First, look at the largest number of your "new" numbering system, divide it
    by 5, and generate your random numbers from 0 to that number. Then take the
    generated random number and multiply it by 5 to find out what it is in your
    new system. For example, let's say the largest number you will work with in
    your new system is 100. Dividing by 5 gives 20. To find a random number in
    your new system, use 5*Int(21*Rnd).

    Second, generate a random number between 0 and the maximum value in your new
    system and pass it through the function described below which rounds numbers
    to the nearest multiple of 5. (Personally, I think the 1st method above will
    yield the "fairer" distribution of random numbers in your new system.)

    This function will round a number to the closest multiple of 5 (so that 12
    rounds to 10 and 13 rounds to 15, etc.). You can feed the function an
    integer, a long, single, double or a string representation of a number; and
    the function returns a Variant so it can be assigned anywhere. If you feed
    it a fractional value, it rounds off at the first decimal place. You can add
    appropriate error checking if you want and are not handling it in your main
    code.

    Function RndToNearest5(Number As Variant) As Variant
    If IsNumeric(Number) Then
    RndToNearest5 = 5 * (Int(CDbl(Number) + 2.5) \ 5)
    Else
    MsgBox "Error -- A valid number was not entered!", vbCritical
    End If
    End Function

    Rick


    "John K." <john.k@home.se> wrote in message news:392ee216@news.devx.com...
    > Hi!
    >
    > Let's say I make up my own numeric system.
    > Instead of 1,2,3,4,5,6........and so on,
    > numbers can only be 1,5,10,15,20......and so on.
    >
    > Then let's say I create a random number, which I
    > want to convert into my system.
    > Let's say I get the number 24.
    > Now, I don't actually know the number is 24.
    > How do I round that number into my system?
    > How should my computer know that it should
    > be turned into 25 instead? Or that 32 should be 30??
    >
    > Thanks!
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center