Results 1 to 15 of 15

Thread: Number of digits

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    384

    Number of digits

    Hi all,

    I have a program and I should enter an intger number, How do I check if this number is a ( 5 digits number )..

    using the mod or "/" operators..

    Thanks in advance,
    zeid

  2. #2
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Number of digits

    expo = 1
    donextexpo = true
    while donextexpo
    'If a num1 Mod num2 doesn't change num1 then num1 < num2.. If num1 Mod num2 = 0 then num1 = integer*num2.. Since we know num1 <= num2 in our scenario then the only possible value integer is 1
    if (x mod 10^expo) = x then
    if x = 10^expo then 0 = expo+1
    donextexpo = false
    endif
    expo=expo+1
    wend
    numdigits = ???

    '--------------------------
    expo = 1
    donextexpo = true
    while donextexpo
    if x div 10^expo < 1 then donextexpo = false
    expo = expo+1
    wend
    numdigits = ???

    There are many different possible implementations using div/mod.. These functions are VITAL to be comfortable with in many types of code.. I highly advise fully understanding this rather than just solving my simple ommisions and handing it in.. I'm purposely leaving code tags out to force you to look through it closer and there is a glaringly hilarious error in there..
    Last edited by triggernum5; Nov 18th, 2006 at 09:43 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    384

    Re: Number of digits

    isn't there any simpler way?

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Number of digits

    VB Code:
    1. Private Sub Command1_Click()
    2.     If IsNumeric(Text1.Text) Then
    3.         If Val(Text1) = Int(Text1) And Len("" & Val(Text1)) = 5 Then
    4.             MsgBox "5 digit number"
    5.         Else
    6.             MsgBox "not a 5 digit number"
    7.         End If
    8.     Else
    9.         MsgBox "not a number"
    10.     End If
    11. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    384

    Re: Number of digits

    I'm using java ... but I should do the calculation using mathmatical operations

  6. #6
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Number of digits

    My second example is about as simple as it gets I think..

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Number of digits

    Code:
    (Typecast_to_Integer) log10(number) + 1
    
    or if log10 is not available
    
    (Typecast_to_Integer) (log(number)/log(10)) + 1
    Show Appreciation. Rate Posts.

  8. #8
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Number of digits

    The (typecast_to_inter) in vb is Int() btw, and Log() is Log10.. (In vb you must understand log laws to convert to another base using Log10..)
    numdigits=Int(Log(x))+1
    If you can use any math functions then I take back what I said about my second method..

  9. #9
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Number of digits

    Quote Originally Posted by triggernum5
    The (typecast_to_inter) in vb is Int() btw, and Log() is Log10.. (In vb you must understand log laws to convert to another base using Log10..)
    numdigits=Int(Log(x))+1
    If you can use any math functions then I take back what I said about my second method..
    This is a Math forum and OP never said he wants answer in VB.

    Secondly, you are mistaken about Int() in VB, it's CInt (Convert expression to Integer type) as opposed to Int (Return the Integer portion of a number).

    Even in VB, Math.Log() returns you the Natural logarithm of a number, means to the base of e, and not the base of 10.
    Show Appreciation. Rate Posts.

  10. #10
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Number of digits

    If num >= 10000 Then is5digit

  11. #11
    Member
    Join Date
    Aug 2006
    Posts
    46

    Re: Number of digits

    i think u would need if Num>=10000 and if Num<=-10000
    Visual Basic 6 Novice

    Learning Direct X 8 for VB....



    http://externalweb.exhedra.com/DirectX4VB/ <--- Great Direct X Tutorials

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    384

    Re: Number of digits

    Thanks any way .. I figured it like this:

    if num >= 10,000 and num < 100,000

  13. #13
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Number of digits

    That and should be an or.

  14. #14
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Number of digits

    Umm, I think you're looking at that boolean wrong jeroen.. Oh, and sorry about my Log base goof for vb.. Just out of curiousity, are you allowed to use ANY mathematical method? Or only div/mod?

  15. #15
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Number of digits

    I meant domocobra's.

    But zeidhaddadin is right.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width