2

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.

Adam
  • 6,041
  • 36
  • 120
  • 208
  • 1
    they do work but you'll need to specify the culture code. A quick search on SO will bring up a lot of solutions – DiskJunky Feb 25 '13 at 21:24

2 Answers2

7

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())
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
0

You should be able to acheive your answer by using the Parse method.

    Dim s As String = "0.7655"
    Dim c As Decimal = Nothing

    c = Decimal.Parse(s)

    Console.WriteLine(c)

You can then use the ToString method to convert back to a string.

Jonathan
  • 83
  • 1
  • 1
  • 10