Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CultureInfo (es) ToDecimal()
Message
De
19/08/2017 04:05:55
 
 
Information générale
Forum:
C#
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01653447
Message ID:
01653492
Vues:
35
This message has been marked as a message which has helped to the initial question of the thread.
Why would you expect to convert "P1111" to decimal ?
Try:
public static long ToLong(this String tcString)
        {
            long result = 0;
            long.TryParse(tcString, out result);
            return result;
        }
>I am now getting this error
>
>System.FormatException: Input string was not in a correct format.
> at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
> at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
> at System.Convert.ToDecimal(String value, IFormatProvider provider)
>
>
>at this line of code
>
>
>  //=====================================================================================================================
>        public static long ToLong(this String tcString)
>        {
>            Decimal dDecimalValue = Convert.ToDecimal(tcString, CultureInfo.InvariantCulture);
>            try { return (Int64)dDecimalValue; }
>            catch { }
>            return 0L;
>        }
>
>when I pass P1111 as the string.
>
>Do you know what is wrong here?
>
>Thanks in advance.
>
>
>>In Spain the use of ',' and '.' is switched when writing currency values. e.g.:
>>
>>$12,500.50 becomes $12.500,50
>>
>>
>>>UPDATE. So, I searched the project for ToDecimal and added CultureInfo.InvariantCulture to all the conversions. I'm going to do that for ToFloat and ToDouble as well now.
>>>
>>>
>>>Hi everybody,
>>>
>>>I have the following lines of code:
>>>
>>>else if (SqlDbType.Decimal == parameterType || SqlDbType.SmallMoney == parameterType)
>>>            {
>>>                if (parameterSize > 0)
>>>                    parameter = new SqlParameter("@" + parameterName, parameterType, parameterSize);
>>>                else
>>>                    parameter = new SqlParameter("@" + parameterName, parameterType);
>>>                parameter.Value = parameterValue.ToDecimal();
>>>            }
>>>
>>>
>>>My parameterValue is a string
>>>
>>>-993.13
>>>
>>>When the last line is executed, I'm getting
>>>
>>>Parameter.Value = + SqlValue {-99313} object {System.Data.SqlTypes.SqlDecimal}
>>>
>>>Prior to executing that code I executed the following:
>>>
>>>
>>>CultureInfo.DefaultThreadCurrentCulture = culture;
>>>                    CultureInfo.DefaultThreadCurrentUICulture = culture;
>>>
>>>                    Thread.CurrentThread.CurrentCulture = culture;
>>>                    Thread.CurrentThread.CurrentUICulture = culture;
>>>
>>>and my culture was 'es'.
>>>
>>>So, apparently in es converting 993.13 to Decimal produced 99313 and the . was ignored.
>>>
>>>I need to treat it as normal decimal value. I'm going to research if ToDecimal allows specifying English culture and looks like we would need to be careful with all parsing and converting of the string values into decimal.
>>>
>>>Thanks a lot in advance.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform