Results 1 to 22 of 22

Thread: Cdbl() bug?

  1. #1

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371

    Cdbl() bug?

    Hi there...

    I've recently tried to convert a string into a double, using Cdbl(), however I think there is a bug in VB6 Pro...

    Here's what I get:

    dim A as string
    dim B as double

    A="123.45"

    if I use it this way,

    B= cdbl(A)

    I get: B=12345 (Wrong!!!)

    If I use this way,

    B= cdbl(123.45)

    I get: B=123.45 (Correct!!)

    Does anybody else had this problem?

    thanks in advance

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    This works for me:
    VB Code:
    1. Dim A As String
    2. Dim B As Double
    3.  
    4. A = "123.45"
    5.  
    6. B = CDbl(A)
    7. MsgBox B
    (I have Enterprise Edition)

    See attachment..

    Phreak
    Attached Images Attached Images  

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    And is there a way to go around the bug??

    I mean .. can I take the " " from the string variable and insert it in the cdbl() ?

  4. #4
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    Post a screenshot like phReAk did
    I cant believe you are getting those answers when noone else is

  5. #5
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Does this work?
    VB Code:
    1. Dim A As String
    2. Dim B As Double
    3.  
    4. A = "123.45"
    5.  
    6. B = CDbl(CInt(Left(A, 3)) + Right(A, 3))
    7. MsgBox B
    It kinda uses integers aswell, but works just the same.

    Does it work for you?

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  6. #6

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    Well I don't have VB right now but I'll get the printscreen as soon as I can...

    to tell you the truth that bug happend when I used an array of strings... when I called one of those variables inside the array I had the bug..

    something like this:

    dim A(10) as string
    dim B as double

    A(2)="123.45"
    B=Cdbl(A(2))

    as for phReAk's code... I don't think is the correct way to use since I chose double variable because I had very large (and no way to now their size) numbers, so I can't use integers...

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Will using the CDbl function fix this?
    VB Code:
    1. Dim A As String
    2. Dim B As Double
    3.  
    4. A = "123.45"
    5.  
    6. B = CDbl(CDbl(Left(A, 3)) + Right(A, 3))
    7. MsgBox B
    ?

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    well... maybe, but the problem is I don't know how long will the number be (in the program) so I'll have to figure out a way to convert it ... (witch isn't that difficult - but it totally messes my code up)...
    ..
    And since there is a function that SHOULD do this I don't see why I shoul develop new code....
    ..
    but for what I'm seeing I'm the only one with this problem... so I'll soon get a print screen and see if there's more to it (??- I doubt)
    ..
    Another thing ... I have no patches installed!! so it's VB6.0 Professional original..

  9. #9
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Believe me if there was a bug with CDBL it would've been noticed before version 6 sp 6...

    I think you'll find there's a problem with your code and you're not passing the number to the CDBL function correctly.
    Why not put a breakpoint on the CDBL command, and when the program hits it use the DEBUG window to discover what is being passed to the function...?
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Maybe instead of CDbl you can use Val? They both return a double...


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Or try using a comma (",") instead of a period (".").


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Stop blaming CDBL - there's nothing wrong with it - post the code you have in your project and we'll take a look.
    Or but a DEBUG.PRINT [variable name] before the CDBL line to see what is actually being passed into the CDBL function.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  13. #13

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    ...
    ... ok... here are the examples...
    ...
    as you can see I kid you not... only the last worked... probably it has to do with assuming "." instead of ",".... but still it is a bug!!!
    Attached Files Attached Files

  14. #14
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    As you can see, the msgbox tells you "123,45" and not "123.45". Try with a comma instead of a dot.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  15. #15
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    This code:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print CDbl("123.45")
    5.     Debug.Print CDbl("123,45")
    6.    
    7. End Sub
    returned:
    12345
    123,45
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  16. #16

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    Well ... the thing is: in my project I import the data from a txt file and I can't control if the data inserted in the txt file is using "." or ","... so when I use the Cdbl(Variable) (being variable the data imported) I would expect it would recognise the data...

    It may not be a problem unless I use a computer with a diferent language of windows installed... If I use the program in Win XP (English version) I would get the oposite... So I would have to know wich version of the windows is installed to be abble to convert right!!!....

    Wich totally SUCKS ...

    That's Why I hate Mcrosoft!!!

    now that I got that out of my chest ... do you know of another function that converts strings to doubles (without bugs)???

    By the way Buzby sometimes they also make mistakes!!

  17. #17
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    It is not a bug!!

    If you regional settings says that the "," is the decimal separator, then the "." would be interpreted as a thousand separator.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  18. #18

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    You may be right about that ... but still He didn't even recognised it as a thousand separator...

    Is there a way to go around that?

  19. #19
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Who says it does not? It did recognize it as a thousand separator. When dealing with number the thousand separator "does not exist". So, "123.456" would be a string value, but its number would be 123456.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  20. #20
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Oh come on... What is so difficult about doing this:

    VB Code:
    1. Public Function CDouble(ByVal strDouble As String) As Double
    2.  Dim iPos As Integer
    3.  Dim strAfterDot As String
    4.  
    5.  strDouble = Trim(strDouble)
    6.  iPos = InStr(1, strDouble, ".")
    7.  
    8.  If iPos > 0 Then
    9.   strAfterDot = Mid(strDouble, iPos + 1)
    10.   CDouble = CInt(Mid(strDouble, 1, iPos)) + CInt(strAfterDot) * 0.1 ^ Len(strAfterDot)
    11.  Else
    12.   CDouble = CInt(strDouble)
    13.  End If
    14. End Function
    15.  
    16. Private Sub Form_Load()
    17.  Debug.Print CDouble("13.22")
    18.  Debug.Print CDouble("15467.09801")
    19.  Debug.Print CDouble("12")
    20. End Sub
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  21. #21

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    thanks for the code... but I'll just use this one:

    private sub solving_what_should_have_already_be_right( Mystring as string)

    MyString=replace(MyString, ".", ",")

    end sub

    and hope that however runs my program has the same regional simbol for decimals...

  22. #22
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Tec-Nico
    Oh come on... What is so difficult about doing this:

    VB Code:
    1. Public Function CDouble(ByVal strDouble As String) As Double
    2.  Dim iPos As Integer
    3.  Dim strAfterDot As String
    4.  
    5.  strDouble = Trim(strDouble)
    6.  iPos = InStr(1, strDouble, ".")
    7.  
    8.  If iPos > 0 Then
    9.   strAfterDot = Mid(strDouble, iPos + 1)
    10.   CDouble = CInt(Mid(strDouble, 1, iPos)) + CInt(strAfterDot) * 0.1 ^ Len(strAfterDot)
    11.  Else
    12.   CDouble = CInt(strDouble)
    13.  End If
    14. End Function
    15.  
    16. Private Sub Form_Load()
    17.  Debug.Print CDouble("13.22")
    18.  Debug.Print CDouble("15467.09801")
    19.  Debug.Print CDouble("12")
    20. End Sub
    As a matter of fact, it should be:

    VB Code:
    1. Public Function CDouble(ByVal strDouble As String) As Double
    2.  Dim iPos As Integer
    3.  Dim strAfterDot As String
    4.  
    5.  strDouble = Trim(strDouble)
    6.  iPos = InStr(1, strDouble, ".")
    7.  
    8.  If iPos > 0 Then
    9.   strAfterDot = Mid(strDouble, iPos + 1)
    10.   CDouble = CInt(Mid(strDouble, 1, iPos)) + CInt(strAfterDot) * 0.1 ^ Len(strAfterDot)
    11.  Else
    12.   CDouble = [b]CDbl[/b](strDouble)
    13.  End If
    14. End Function
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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