Convert this string to decimal

It sounds simple, but when I tried to achieve, I know how to format this conversion, these are some examples of strings that I need to convert to decimal

00.24
48.34
01.24

Does anyone know how I can accomplish this? I tried like this

try
{
   decimal x =  Convert.ToDecimal("00.24", );
   //Which formatter do I need to pass??
   decimal x =  Convert.ToDecimal("00.24", Formatter???);
}
Catch(Exception e)
{
    throw new Exception()
}

But this does not work, because the result is 24D and I need 0.24D

+1
source share
4 answers

I suspect that your system culture is not English and has different rules for formatting numbers. Try to convey an invariant culture as a format provider:

decimal d = Convert.ToDecimal("00.24", CultureInfo.InvariantCulture);

You can also use Decimal.Parse:

decimal d = Decimal.Parse("00.24", CultureInfo.InvariantCulture);
+5
source

Why not just use Decimal.Parse

decimal x = Decimal.Parse("00.24");
Console.WriteLine(x);  // Prints: 00.24
+3
source

, Decimal.TryParse . .

+2

, , , . (). . , "20.100.200" "1.2.3.4" 20100200 1234.

, 'es'

, . , . CultureInfo.InvariantCulture - ( en-US).

+1

All Articles