Prevent Auto Rounding

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

Hello,
Let's say Textbox1.text = 10
size = 46

answer = (Val(TextBox1.Text) * 100.5) / size
TextBox2.Text = Format(answer, "#######.000")

The answer is: 21.847830...
However, when I get the result in textbox2.text - it shows:
21.848

While I understand that this is rounding up - how can I
make it so that I still get the 3 decimal places - but it
shows:
21.847
(without the rounding)
 
Hi Keith,

Microsoft has used the International Banking standard. When you ask in this
newsgroup who is using that than there comes no answer. You have to do
rounding with your own routines.

See here the questions and answer in the language.vb which has been about
rounding.

http://tinyurl.com/353eu

I hope this helps?

Cor
 
Keith,

You have 2 options, use the INSTR and LEFT functions OR multiply the value
up, convert it using the INT function then divide it back down. See
examples below;

Option 1:
Dim Answer as String
Dim Ptr As Integer


Answer = CStr( (10 * 100.5) / 46) 'Calculate your value and
convert to a string
Ptr = Instr(Answer, ".") 'Check if the result
has a DECIMAL
If (Ptr = 0) Then 'If there is NOT a
decimal
TextBox2.Text = Answer 'Display the answer
as-is
Else 'Otherwise
TextBox2.Text = Left(Answer, Instr + 3) 'Display the string up
to the Decimal + next 3 characters
End If

Option 2:
Dim Answer As Double

Answer = (10 * 100.5) / 46
Answer = Answer * 1000
Answer = Int(Answer)
Answer = Answer / 1000
TextBox2.Text = Answer.ToString

NOTE: In Option 2 you could perform all the math on 1 line as in;
TextBox2.Text = (Int(((10 * 100.5) / 46) * 1000) / 1000).ToString
 
Back
Top