Muj - 2 months ago 13

Vb.net Question

Hello :) I have a formula like this

`Dim numbHolder as Decimal = 'Anything i will send'`

If val(txtbox1.text) <= 9 then

txtbox2.text = numbHolder * (0.0 & val(txtbox1.text))

else

txtbox2.text = numbHolder * (0. & val(txtbox1.text))

end if

is there an easiest way or function that i can use to multiply any number in percentage?

because in my scenario i have to check it first if the number is not greater than 9

so that i can use correct formula like

`0.09`

`0.9`

Answer

If I were you I would avoid mixing `Val`

and calculations in the one place. Try separating the parsing and populating text fields with the calculation like this:

```
' Parse/Load Values
Dim numbHolder As Decimal = 10 'Anything i will send'
Dim percentage As Decimal = CType(Val(txtbox1.Text), Decimal) / 100
' Calculate
Dim result = numbHolder * percentage
' Populate
txtbox2.Text = result.ToString()
```