Answered by:
The number changes

Question
-
User917353587 posted
Hi, friends.
My code is
TextBox2.Text = Val(TextBox1.Text) * 0.6
When I go to another row the value 0.6 changes to 0.599999999999998 ?!?
How to fix 0.6 to be 0.6 ?
Regards
Thursday, October 26, 2017 1:58 PM
Answers
-
User347430248 posted
Hi vesodimov,
if you see your code then you will find that you wrote the 0.6 /100/4 together with in the round bracket of Val(TextBox3.text).
I suggest you to write it outside the round bracket.
like below.
TextBox4.Text = Val(TextBox1.Text) * Val(TextBox3.Text) * 0.6 / 100 / 4
currently , it looks like it is first trying to calculate the values with in the bracket. so it is possible that it is displaying you the multiplication of value stored in Textbox3 and 0.6.
other thing is that you can not calculate with string value.
you can see that Textbox3 is string value and you perform the calculation inside the round bracket. so in that situation it is not get converted in the number value.
so I suggest you to first covert the textbox value to number and then try to do other calculation outside round bracket.
try to make changes and make test on your side. let us know whether it solves your issue or not.
further if you want to do Val(TextBox3.Text) * 0.6 / 100 / 4 first then you can add bracket like below.
TextBox4.Text = Val(TextBox1.Text) * (Val(TextBox3.Text) * 0.6 / 100 / 4)
Regards
Deepak
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 27, 2017 7:58 AM
All replies
-
User2103319870 posted
vesodimov
When I go to another row the value 0.6 changes to 0.599999999999998 ?!?
How to fix 0.6 to be 0.6 ?
Try using Math.Round
TextBox2.Text = Math.Round(Convert.ToDecimal(Val(TextBox1.Text) * 0.6), 1, MidpointRounding.AwayFromZero)
Thursday, October 26, 2017 2:39 PM -
User917353587 posted
Uauuu. Such a formula for one number ?
But why 0.6 changes to 0.5999999999998 ?
Friday, October 27, 2017 5:02 AM -
User347430248 posted
Hi vesodimov,
I try to test the same code on my side.
you asked that 0.6 changes to 0.5999999999998 when you move to other fields.
below is the output I got.
you can see in the output that 0.6 not changes to 0.5999999999998.
how you produce the issue. if I miss any step then inform me know about it.
so when you use math.round it will round the value and show you with specific decimal places.
you can say that the accurate answer fro your calculation is 0.5999999999998.
but to understand it easily you can round the value using this function.
Regards
Deepak
Friday, October 27, 2017 7:01 AM -
User917353587 posted
Thank you, Deepak.
This happens on my code, not in result:
In this place I write 0.6 not 0.599999999999998, but when I move my cursor to other place number changes to 0.59999999999999998 ?!?
Friday, October 27, 2017 7:42 AM -
User347430248 posted
Hi vesodimov,
if you see your code then you will find that you wrote the 0.6 /100/4 together with in the round bracket of Val(TextBox3.text).
I suggest you to write it outside the round bracket.
like below.
TextBox4.Text = Val(TextBox1.Text) * Val(TextBox3.Text) * 0.6 / 100 / 4
currently , it looks like it is first trying to calculate the values with in the bracket. so it is possible that it is displaying you the multiplication of value stored in Textbox3 and 0.6.
other thing is that you can not calculate with string value.
you can see that Textbox3 is string value and you perform the calculation inside the round bracket. so in that situation it is not get converted in the number value.
so I suggest you to first covert the textbox value to number and then try to do other calculation outside round bracket.
try to make changes and make test on your side. let us know whether it solves your issue or not.
further if you want to do Val(TextBox3.Text) * 0.6 / 100 / 4 first then you can add bracket like below.
TextBox4.Text = Val(TextBox1.Text) * (Val(TextBox3.Text) * 0.6 / 100 / 4)
Regards
Deepak
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 27, 2017 7:58 AM -
User917353587 posted
Ahaaa !!!
When I change position of brackets /like your code/ - all work. 0.6 stay 0.6.
Thank you, Deepak. Again ;-)
Friday, October 27, 2017 8:57 AM -
User917353587 posted
Bad ! Again !
Tuesday, October 31, 2017 12:11 PM -
User347430248 posted
Hi vesodimov,
I suggest you to store the value in variable and try to use that variable in your calculation to avoid the issue.
you can also try to declare the variable as constant.
so value will not get change.
You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value.
example:
Public Const DaysInYear = 365 Private Const WorkDays = 250
Reference:
How to: Declare A Constant (Visual Basic)
Regards
Deepak
Wednesday, November 1, 2017 5:12 AM -
User917353587 posted
Thank you, Deepak ... again ;-)
I declare 0.6 like const K1:
Public Const K1 = 0.6
and put K1 in to my formula. I think that work .
Regards
Wednesday, November 1, 2017 7:11 AM