Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I change / convert the result of the division or multiplication
examples of the division = 200.1247 becomes 200.12

I use this coding
Please also corrected my code is


VB
Private Sub Cover()

Const Konstan As Single = 1000

WG1.Text = Val(QP1.Text) * Val(BM1.Text)
            TW1.Text = (Val(WG1.Text) + Val(WG6.Text) + Val(WG11.Text))
            GI1.Text = (Val(WG1.Text) / Val(TW1.Text)) * Val(QL1.Text) * Konstan
            'Cover L
            WG6.Text = Val(QP2.Text) * Val(BM6.Text)
            GI6.Text = (Val(WG6.Text) / Val(TW1.Text)) * Val(QL1.Text) * Konstan
            'Cover R
            WG11.Text = Val(QP3.Text) * Val(BM11.Text)
            GI11.Text = (Val(WG11.Text) / Val(TW1.Text)) * Val(QL1.Text) * Konstan
End Sub
Posted

1 solution

This all is so wrong so I don't know how to write about it. This short fragment shows that you tend to invite disasters. Just stop it.

You need a habit to perform arithmetic calculations with numeric types, never with texts. You need to declare all numeric variables explicitly, then convert you UI texts to the numeric values, perform calculations and finally populate UI with the result of calculations. All the numeric variables I mentioned above should be in one separate class or structure. All three steps should be three separate methods: 1) UI to data, 2) calculations with data, 3) data to UI.

Now, your question itself is completely unclear. If you think that you need to round value 200.1247 to 200.12, think again. Rounded is never needed, well, almost never. If you need to present rounded values in UI, this is completely different thing which does not require rounding operation itself. You just need to call ToString or string.Format with appropriate format specifiers. If you avoid explicit rounding, it can guarantee you that a rounded value cannot sneak into calculations and cause loss of precision.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900