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.
Printable View
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.
One way would be to use the left function.
Something like this:
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.Code:x = CDbl(Left(CStr(x), 4))
Try the Round() function, if such a thing exists...:p
VB Code:
Int(.6574*100)/100
:)
Not getting a solution. crptcblade Int function is not for what I want. it changes the value.
I'm not sure what you mean. I put my code in, and it return 0.65, just like you asked for.
:confused:
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????
http://msdn.microsoft.com/library/en...asp?frame=true :DQuote:
Originally posted by rjlohan
Try the Round() function, if such a thing exists...:p
I know there is a Round Function but I dont want it.Quote:
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)
Well use my first suggestion then.
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
I had that same problem in a different situation. I posted a thread, which like all my threads got about 3 views:rolleyes: . I had a similar problem with:Quote:
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????
. The first time i run that, it i get 42. Then 41 until i exit VB and restart it.VB Code:
MsgBox Int(0.42*100)
You could do that with mine too...:rolleyes:
run this:Quote:
Originally posted by rjlohan
You could do that with mine too...:rolleyes:
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. :rolleyes:
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.
:rolleyes:
Thanks everyone!!!
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. :rolleyes:Quote:
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.
:rolleyes:
Can we stop arguing before my eyes fall out of my head?
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.
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.
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.
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.
Thanks Hobo, it works perfect!
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.
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.Quote:
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.
Geez Hobo, for someone who doesn't want to argue, you sure do alot of arguing...:rolleyes:
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.
Guys...let the thread die already....
Ok, nishantp.
:p
Hmmm.... should've waited a bit for that one...
That was the other day. I was tired. Today is a new day.Quote:
Geez Hobo, for someone who doesn't want to argue, you sure do alot of arguing...:rolleyes:
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.Quote:
you should be aware that writing a function for everything isn't always or often the best solution to a problem. [/B]
I can go as long as you can...:rolleyes: