vb.netmaskedtextbox

Value from masked text box getting rounded


I am trying to set the value of a Single to the converted value of a Masked Text Box.

My Masked Text Box's mask is "00.000000°", meaning it fills in up to 6 decimal points and displays it as degrees. So say my initial value is 40.12345, it will show "40.123450°".

This is my line of code to convert it, where d is a Single and msk is the Masked Text Box:

d = CSng(msk.Text.Replace("°", "").Trim)

For whatever reason, though, d's value ends up being the contents of the box but rounded to the nearest 5 decimal points! So say I change it to 40.123457, the value of d ends up being "40.12346".

I do not know why this is happening.


Solution

  • A Single has low precision. If you want a precise number, I would suggest you use Decimal instead.

    Dim d As Decimal
    
    d = Decimal.Parse("40.123457°".Replace("°", "").Trim)