|
-
Feb 15th, 2002, 10:57 PM
#1
Thread Starter
Addicted Member
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.
-
Feb 15th, 2002, 11:03 PM
#2
PowerPoster
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]
-----------------------------------------
-
Feb 15th, 2002, 11:05 PM
#3
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 15th, 2002, 11:19 PM
#4
Thread Starter
Addicted Member
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.
-
Feb 15th, 2002, 11:23 PM
#5
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
-
Feb 15th, 2002, 11:26 PM
#6
Thread Starter
Addicted Member
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.
-
Feb 15th, 2002, 11:27 PM
#7
Member
Originally posted by rjlohan
Try the Round() function, if such a thing exists...
http://msdn.microsoft.com/library/en...asp?frame=true
-
Feb 15th, 2002, 11:30 PM
#8
Thread Starter
Addicted Member
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.
-
Feb 15th, 2002, 11:32 PM
#9
Stuck in the 80s
This does no rounding, just gives you two places after the decimal. There's probably a more efficient way:
VB Code:
Dim x As Integer, myNum As Double
myNum = 0.65457
x = InStr(1, myNum, ".")
MsgBox Mid(myNum, 1, x + 2)
-
Feb 15th, 2002, 11:32 PM
#10
PowerPoster
Well use my first suggestion then.
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Feb 15th, 2002, 11:35 PM
#11
Stuck in the 80s
You can even take my example and turn it into a easy to use function:
VB Code:
Private Sub Form_Load()
MsgBox ChopDec(1230.124365457, 2)
End Sub
Private Function ChopDec(dNum As Double, intPlaces) As Double
Dim x As Integer
x = InStr(1, dNum, ".")
ChopDec = Mid(dNum, 1, x + intPlaces)
End Function
-
Feb 15th, 2002, 11:37 PM
#12
Frenzied Member
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:
. 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.
-
Feb 15th, 2002, 11:37 PM
#13
PowerPoster
You could do that with mine too...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Feb 15th, 2002, 11:40 PM
#14
Stuck in the 80s
Originally posted by rjlohan
You could do that with mine too...
run this:
VB Code:
Dim num As Double
num = 12.4242
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.
-
Feb 15th, 2002, 11:42 PM
#15
PowerPoster
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]
-----------------------------------------
-
Feb 15th, 2002, 11:42 PM
#16
Thread Starter
Addicted Member
Ideas are dime a dozen.
People who put them into action are priceless.
-
Feb 15th, 2002, 11:45 PM
#17
Stuck in the 80s
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?
-
Feb 15th, 2002, 11:48 PM
#18
PowerPoster
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]
-----------------------------------------
-
Feb 15th, 2002, 11:49 PM
#19
Stuck in the 80s
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.
-
Feb 15th, 2002, 11:51 PM
#20
PowerPoster
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]
-----------------------------------------
-
Feb 15th, 2002, 11:54 PM
#21
Stuck in the 80s
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.
-
Feb 16th, 2002, 01:01 AM
#22
Thread Starter
Addicted Member
Thanks Hobo, it works perfect!
Ideas are dime a dozen.
People who put them into action are priceless.
-
Feb 17th, 2002, 09:26 AM
#23
Fanatic Member
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.
-
Feb 17th, 2002, 04:11 PM
#24
PowerPoster
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]
-----------------------------------------
-
Feb 17th, 2002, 08:58 PM
#25
Stuck in the 80s
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.
-
Feb 17th, 2002, 09:05 PM
#26
PowerPoster
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]
-----------------------------------------
-
Feb 17th, 2002, 11:17 PM
#27
Frenzied Member
Guys...let the thread die already....
You just proved that sig advertisements work.
-
Feb 17th, 2002, 11:24 PM
#28
PowerPoster
Ok, nishantp.
Hmmm.... should've waited a bit for that one...
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Feb 18th, 2002, 03:14 PM
#29
Stuck in the 80s
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|