51/2=26?
Why visual basic rounds up this calculation. I want it to round it down in order to get the answer 25.
How must I do in order to make vb rounding the calculation down.
I have made like this:
dim result as integer
x=51
result=x/2
Printable View
51/2=26?
Why visual basic rounds up this calculation. I want it to round it down in order to get the answer 25.
How must I do in order to make vb rounding the calculation down.
I have made like this:
dim result as integer
x=51
result=x/2
Try this instead.This is known as integer division and will always return the lower value.Code:Debug.Print 51 \ 2
OK. I solved that declaring result as double
So it will calculate that 25,5. That's fine.
But if I want to do 50/2 the result will be 25. How can I show the decimal. How I get the result 25,0.
If I understand your question, you could use the Format command, when displaying the answer.
eg txtBox = format(50/2,"#,##0.00")
and txtbox = format(51/2,"#,##0.00")
HTH
int or fix should do that. Depending on the question what you want to do if you're dividing something that return negative values:
dim retval as integer
retval = int(51/2) ' returns 25
retval = fix(51/2) ' returns 25
retval = int(-51/2) ' returns -26
retval = fix(-51/2) ' returns -25
Why don't you just use a Double data type?
i.e.
Dim retval As Double
I did as jp adviced and used format control. That's was the solution my problem. First I declared the result variable as double as Pinky said. Thanks to all helped me.
glad to hear you came right.