Results 1 to 29 of 29

Thread: May be an easy one

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254

    Post May be an easy one

    I have 0.6574 number but I only want only 0.65. I tried to do with format but it rounds the number, it returns me 0.66 instead, which I don't want.

    Please advise and thanks in advance.
    Ideas are dime a dozen.
    People who put them into action are priceless.

  2. #2
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    One way would be to use the left function.

    Something like this:
    Code:
    x = CDbl(Left(CStr(x), 4))
    I'm operating at minimum efficiency right now, so that's not gonna be the best way, but it will do ya' til someone else gives you a better one.

    Try the Round() function, if such a thing exists...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Int(.6574*100)/100

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    Not getting a solution. crptcblade Int function is not for what I want. it changes the value.
    Ideas are dime a dozen.
    People who put them into action are priceless.

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I'm not sure what you mean. I put my code in, and it return 0.65, just like you asked for.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    if I have 0.61 it gives me 0.61 at the first time and after that I get 0.60 I just do not know why. I closed my form, open it again, still 0.60. I closed my project. open it again, first time 0.61 and then back to 0.60.

    Just no idea why????
    Ideas are dime a dozen.
    People who put them into action are priceless.

  7. #7
    Originally posted by rjlohan
    Try the Round() function, if such a thing exists...
    http://msdn.microsoft.com/library/en...asp?frame=true

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    I know there is a Round Function but I dont want it.
    Ideas are dime a dozen.
    People who put them into action are priceless.

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This does no rounding, just gives you two places after the decimal. There's probably a more efficient way:

    VB Code:
    1. Dim x As Integer, myNum As Double
    2.  
    3.   myNum = 0.65457
    4.  
    5.   x = InStr(1, myNum, ".")
    6.   MsgBox Mid(myNum, 1, x + 2)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Well use my first suggestion then.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You can even take my example and turn it into a easy to use function:

    VB Code:
    1. Private Sub Form_Load()
    2.   MsgBox ChopDec(1230.124365457, 2)
    3. End Sub
    4.  
    5. Private Function ChopDec(dNum As Double, intPlaces) As Double
    6. Dim x As Integer
    7.  
    8.   x = InStr(1, dNum, ".")
    9.   ChopDec = Mid(dNum, 1, x + intPlaces)
    10. End Function
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by rmoghal
    if I have 0.61 it gives me 0.61 at the first time and after that I get 0.60 I just do not know why. I closed my form, open it again, still 0.60. I closed my project. open it again, first time 0.61 and then back to 0.60.

    Just no idea why????
    I had that same problem in a different situation. I posted a thread, which like all my threads got about 3 views . I had a similar problem with:
    VB Code:
    1. MsgBox Int(0.42*100)
    . The first time i run that, it i get 42. Then 41 until i exit VB and restart it.
    You just proved that sig advertisements work.

  13. #13
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    You could do that with mine too...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by rjlohan
    You could do that with mine too...
    run this:
    VB Code:
    1. Dim num As Double
    2.  
    3.   num = 12.4242
    4.   MsgBox CDbl(Left(CStr(num), 4))

    If he only wants two digits after the decimal, you're code will only work if the number before the decimal is no higher than 9.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    I'm aware of that, but high integer values weren;t part of the problem proposed, so I ignored them.

    Obviously, there would be a need to change it for that case.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    Thanks everyone!!!
    Ideas are dime a dozen.
    People who put them into action are priceless.

  17. #17
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by rjlohan
    I'm aware of that, but high integer values weren;t part of the problem proposed, so I ignored them.

    Obviously, there would be a need to change it for that case.
    Wouldn't it be a good programming practice to create code that will work later without having to be changed? I was preparing for the future.

    Can we stop arguing before my eyes fall out of my head?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  18. #18
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Depends on alot of things. If it's unnecessary to spend time 'preparing for the future' than project costs will often limit this kind of consideration.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  19. #19
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    It'd take about three seconds to make it to be able to be modified. Again, I say, stop the argument. I don't care and I'm too tired.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  20. #20
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Not all considerations for the future only take 3 seconds. As a professional software designer (if ever you are one), the consideration for cost and time (i.e quantity) will quite supercede the consideration for quality.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  21. #21
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Shut up. I'm tired of being nice. My code was more efficient than yours, whether your going to admit it or not. Since your incapable of ending an argument, I'm going to cease responding to you now. Goodnight.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    254
    Thanks Hobo, it works perfect!
    Ideas are dime a dozen.
    People who put them into action are priceless.

  23. #23
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    I'm sorry, but Hobo is right. If you've read any good programming book (not on a certain language, but in general, like Code Complete), then you should know that actual coding time is only a small fraction of total development of a product. Debugging is a huge part (and your solution has a bug of not allowing numbers >9.99999...), as well as modification. Time spent making code solid is an excellent investment--not an area to glaze over.

  24. #24
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    I'm aware of that. But if you were never going to have numbers over 1 then it would have made no difference, and using a function would have been pointless.
    It's up to the designer or at least the design requirements to say which is necessary. If he was using this once in his program, then it would not be efficient to write a long-winded function to do a 1 line job.
    I'm not saying one way or the other which is better, only that design considerations determine the best coding option to use.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  25. #25
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by rjlohan
    I'm aware of that. But if you were never going to have numbers over 1 then it would have made no difference, and using a function would have been pointless.
    It's up to the designer or at least the design requirements to say which is necessary. If he was using this once in his program, then it would not be efficient to write a long-winded function to do a 1 line job.
    I'm not saying one way or the other which is better, only that design considerations determine the best coding option to use.
    But as I am not the developer, I tried to provide him code that would work in all possible situations since I did not know the type of numbers that he was running through.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  26. #26
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Geez Hobo, for someone who doesn't want to argue, you sure do alot of arguing...


    I'm not saying there's anything wrong with your code, but you should be aware that writing a function for everything isn't always or often the best solution to a problem.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  27. #27
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Guys...let the thread die already....
    You just proved that sig advertisements work.

  28. #28
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Ok, nishantp.





    Hmmm.... should've waited a bit for that one...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  29. #29
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Geez Hobo, for someone who doesn't want to argue, you sure do alot of arguing...
    That was the other day. I was tired. Today is a new day.

    you should be aware that writing a function for everything isn't always or often the best solution to a problem. [/B]
    Never did I say it was. I said in this case, since I didn't know the variety of the numbers he'd be punching in, I supplied an example that would be sufficient for any number.

    I can go as long as you can...
    My evil laugh has a squeak in it.

    kristopherwilson.com

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