Ejemplo n.º 1
0
 /// <summary>
 /// Convert a string representation to a Decimal instance.
 /// </summary>
 /// <param name="str">String representation of the Decimal instance.</param>
 /// <returns>Decimal instance created from string representation.</returns>
 public static Decimal StringToDecimal(string str)
 {
     try
     {
         return((Decimal)_dc.ConvertFrom(null, CultureInfo.InvariantCulture, str));
     }
     catch (NotSupportedException)
     {
         // Failed to convert using invariant culture, try again using local culture.
         // This is done because the use of the invariant was added as a fix and so
         // old configurations will have culture specific values.
         return((Decimal)_dc.ConvertFrom(null, CultureInfo.CurrentCulture, str));
     }
 }
Ejemplo n.º 2
0
        public static void ConvertFromInt32()
        {
            // Arrange
            var converter = new DecimalConverter();

            // Act
            var result = converter.ConvertFrom(100);

            // Assert
            Assert.Equal(100M, result);
        }
Ejemplo n.º 3
0
        public override object ConvertFrom(object sourceValue, Type destinationType, IFormatProvider formatProvider, bool ignoreCase)
        {
            if (sourceValue == null)
            {
                return(null);
            }
            if (sourceValue.GetType() == typeof(decimal))
            {
                return(new Money((decimal)sourceValue));
            }

            DecimalConverter dc = new DecimalConverter();

            return(new Money((decimal)dc.ConvertFrom(sourceValue)));
        }