public void ConvertToMoney() { var builder = new CurrencyBuilder("EUR", "HighPrecision"); builder.LoadDataFromCurrency(Currency.FromCode("EUR")); builder.DecimalDigits = 4; var nm = new NodaMoney.Money(1234.5678m, builder.Build()); var m = nm.ToCtpMoney(); Assert.IsType <Money>(m); Assert.Equal(123457, m.CentAmount); Assert.Equal("EUR", m.CurrencyCode); }
/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent. A return value indicates whether the conversion succeeded or failed.</summary> /// <param name="value">The string representation of the money to convert.</param> /// <param name="result">When this method returns, contains the <see cref="Money"/> value that is equivalent to the money /// value contained in <i>value</i>, if the conversion succeeded, or is Money value of zero with no currency (XXX) if the /// conversion failed. The conversion fails if the <i>value</i> parameter is <b>null</b> or <see cref="String.Empty"/>, is not a number /// in a valid format, or represents a number less than <see cref="Decimal.MinValue"/> or greater than <see cref="Decimal.MaxValue"/>. This parameter is passed /// uninitialized; any <i>value</i> originally supplied in result will be overwritten</param> /// <returns><b>true</b> if <i>value</i> was converted successfully; otherwise, <b>false</b>.</returns> /// <remarks>See <see cref="Decimal.TryParse(String, out Decimal)"/> for more info and remarks.</remarks> public static bool TryParse(string value, out Money result) { if (string.IsNullOrWhiteSpace(value)) { result = new Money(0, Currency.FromCode("XXX")); return false; } Currency currency; try { currency = ExtractCurrencyFromString(value); } catch (FormatException) { result = new Money(0, Currency.FromCode("XXX")); return false; } return TryParse(value, NumberStyles.Currency, GetNumberFormatInfo(currency, null), currency, out result); }
/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent. A return value indicates whether the conversion succeeded or failed.</summary> /// <param name="value">The string representation of the money to convert.</param> /// <param name="style">A bitwise combination of enumeration values that indicates the permitted format of value. A typical value to specify is <see cref="NumberStyles.Currency"/>.</param> /// <param name="provider">An object that supplies culture-specific parsing information about <i>value</i>.</param> /// <param name="currency">The currency to use for parsing the string representation.</param> /// <param name="result">When this method returns, contains the <see cref="Money"/> value that is equivalent to the money /// value contained in <i>value</i>, if the conversion succeeded, or is Money value of zero with no currency (XXX) if the /// conversion failed. The conversion fails if the <i>value</i> parameter is <b>null</b> or <see cref="String.Empty"/>, is not a number /// in a valid format, or represents a number less than <see cref="Decimal.MinValue"/> or greater than <see cref="Decimal.MaxValue"/>. This parameter is passed /// uninitialized; any <i>value</i> originally supplied in result will be overwritten</param> /// <returns><b>true</b> if <i>value</i> was converted successfully; otherwise, <b>false</b>.</returns> /// <remarks>See <see cref="Decimal.TryParse(String, NumberStyles, IFormatProvider, out Decimal)"/> for more info and remarks.</remarks> public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, Currency currency, out Money result) { decimal amount; bool isParsingSuccessful = decimal.TryParse(value, style, GetNumberFormatInfo(currency, provider), out amount); if (isParsingSuccessful) { result = new Money(amount, currency); return true; } result = new Money(0, Currency.FromCode("XXX")); return false; }
/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent. A return value indicates whether the conversion succeeded or failed.</summary> /// <param name="value">The string representation of the money to convert.</param> /// <param name="currency">The currency to use for parsing the string representation.</param> /// <param name="result">When this method returns, contains the <see cref="Money"/> value that is equivalent to the money /// value contained in <i>value</i>, if the conversion succeeded, or is Money value of zero with no currency (XXX) if the /// conversion failed. The conversion fails if the <i>value</i> parameter is <b>null</b> or <see cref="String.Empty"/>, is not a number /// in a valid format, or represents a number less than <see cref="Decimal.MinValue"/> or greater than <see cref="Decimal.MaxValue"/>. This parameter is passed /// uninitialized; any <i>value</i> originally supplied in result will be overwritten</param> /// <returns><b>true</b> if <i>value</i> was converted successfully; otherwise, <b>false</b>.</returns> /// <remarks>See <see cref="Decimal.TryParse(String, out Decimal)"/> for more info and remarks.</remarks> public static bool TryParse(string value, Currency currency, out Money result) { return TryParse(value, NumberStyles.Currency, GetNumberFormatInfo(currency, null), currency, out result); }
/// <summary>Converts the value of this instance to an <see cref="decimal"/>.</summary> /// <param name="money">A <see cref="Money"/> value.</param> /// <returns>The value of the <see cref="Money"/> instance, converted to a <see cref="decimal"/>.</returns> /// <remarks>The <see cref="Currency"/> information is lost.</remarks> public static decimal ToDecimal(Money money) { return money.Amount; }
/// <summary>Converts the value of this instance to an <see cref="float"/>.</summary> /// <param name="money">A <see cref="Money"/> value.</param> /// <returns>The value of the <see cref="Money"/> instance, converted to a <see cref="float"/>.</returns> /// <remarks>Because a <see cref="float"/> has fewer significant digits than a <see cref="Money"/> value, this operation may /// produce round-off errors. Also the <see cref="Currency"/> information is lost.</remarks> public static float ToSingle(Money money) { return Convert.ToSingle(money.Amount); }
/// <summary>Converts the value of this instance to an <see cref="double"/>.</summary> /// <param name="money">A <see cref="Money"/> value.</param> /// <returns>The value of the current instance, converted to a <see cref="double"/>.</returns> /// <remarks>Because a Double has fewer significant digits than a <see cref="Money"/> value, this operation may produce round-off /// errors.</remarks> public static double ToDouble(Money money) { return Convert.ToDouble(money.Amount); }
public static HighPrecisionMoney ToCtpHighPrecisionMoney(this NodaMoney.Money money, MidpointRounding midpointRounding = MidpointRounding.ToEven) { return(HighPrecisionMoney.FromDecimal(money.Currency.Code, money.Amount, (int)money.Currency.DecimalDigits, midpointRounding)); }
public static Money ToCtpMoney(this NodaMoney.Money money, MidpointRounding midpointRounding = MidpointRounding.ToEven) { return(Money.FromDecimal(money.Currency.Code, money.Amount, midpointRounding)); }