0

I want to split a double into parts, without rounding.

Tried to use int and fix but they are giving strange results.

MsgBox Int(17.82 / 1.62) 'gives me 10 but 
MsgBox 17.82 / 1.62 'gives me 11

I want results like 11 , but then how to use int or fix functions?

GSerg
  • 76,472
  • 17
  • 159
  • 346
user1115284
  • 71
  • 3
  • 14
  • You sure you want without rounding? `Int` truncates, which is "without rounding". `CInt` rounds which gives you 11 (because `17.82 / 1.62` gives a number which [is just below `11`](http://stackoverflow.com/q/2100490/11683).). – GSerg Jan 12 '13 at 10:07
  • Thanks for your reply. Actually I want the decimal part as it is. I am using that part in next calculation. so if INT truncates, why it gives 10, it should give 11 and truncate zero from decimal portion. can you please explain.... – user1115284 Jan 12 '13 at 10:18
  • Click the link in my previous comment. Your expression yields 10.9999999999999 ish. If you don't want that and if the fractional part of your numbers is not going to be long, then use fixed-point data types, such as `MsgBox Int(CCur(17.82) / CCur(1.62))`. – GSerg Jan 12 '13 at 10:19
  • Ok and thanks. so is there any trick I can do, to get 11, as I need this kind of result? – user1115284 Jan 12 '13 at 10:25
  • Yes, fixed point arithmetic with `Currency` or `Decimal` data types. See above. Make sure the fractional part is short enough first. – GSerg Jan 12 '13 at 10:28

2 Answers2

0

First of all read this: MSDN: Arithmetic Operators in Visual Basic

Then look at this:

Private Sub Form_Load()
    MsgBox 17.82 \ 1.62 ' returns 9
    MsgBox 17.82 / 1.62 ' returns 11
End Sub
mllamazares
  • 7,876
  • 17
  • 61
  • 89
0

You asked for a trick. Yes the simplest and easiest trick is to add 0.5 to the dividend i.e. numerator

MsgBox Int((17.82 + 0.5) / 1.62) 'should give 11

Use this only if you want to use the INT function

user1947746
  • 166
  • 2
  • 5