Convert string to decimal to string and back to decimal

17,196

In your current culture the decimal separator might be a comma instead of a dot. Using the invariant culture will always use the dot:

Dim s As String = "0.7655"

Dim decValue As Decimal = Decimal.Parse(s, System.Globalization.CultureInfo.InvariantCulture)
Console.WriteLine(decValue.ToString())
Share:
17,196
Adam
Author by

Adam

Updated on June 05, 2022

Comments

  • Adam
    Adam almost 2 years

    I want to convert a string to a decimal then to a string and then to a decimal again.

    I tried:

    Dim s As String = "0.7655"
    
    CDec(sDec).ToString("0.00")  'results in: 7653,00
    CDec(sDec).ToString 'results in: 7648
    CDec(sDec).ToString("N") 'results in: 7.653,00
    

    So none of these work!

    Is there no easy function to just convert the exact decimal to its string representation again? Seems like too much work for such a simple task!

    Preferably without formatting the string, because in that case I seem to know beforehand how many characters the resulting string should have. After converting it to string, I also want to convert it back to a decimal again.