Prevent Auto Rounding

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)
 
C

Cor Ligthert

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
 
I

Ian Mooney

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top