本文整理汇总了C#中MidpointRounding类的典型用法代码示例。如果您正苦于以下问题:C# MidpointRounding类的具体用法?C# MidpointRounding怎么用?C# MidpointRounding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MidpointRounding类属于命名空间,在下文中一共展示了MidpointRounding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NumberToCurrencyText
public static string NumberToCurrencyText(decimal number, MidpointRounding midpointRounding = MidpointRounding.ToEven)
{
// Round the value just in case the decimal value is longer than two digits
number = Decimal.Round(number, 2, midpointRounding);
string wordNumber = String.Empty;
// Divide the number into the whole and fractional part strings
string[] arrNumber = number.ToString().Split('.');
// Get the whole number text
long wholePart = Int64.Parse(arrNumber[0]);
string strWholePart = NumberToText(wholePart);
// For amounts of zero dollars show 'No Dollars...' instead of 'Zero Dollars...'
wordNumber = (wholePart == 0 ? "No" : strWholePart) + (wholePart == 1 ? " Dollar and " : " Dollars and ");
// If the array has more than one element then there is a fractional part otherwise there isn't
// just add 'No Cents' to the end
if (arrNumber.Length > 1)
{
// If the length of the fractional element is only 1, add a 0 so that the text returned isn't,
// 'One', 'Two', etc but 'Ten', 'Twenty', etc.
long fractionPart = Int64.Parse((arrNumber[1].Length == 1 ? arrNumber[1] + "0" : arrNumber[1]));
string strFarctionPart = NumberToText(fractionPart);
wordNumber += (fractionPart == 0 ? " No" : strFarctionPart) + (fractionPart == 1 ? " Cent" : " Cents");
}
else
wordNumber += "No Cents";
return wordNumber;
}
示例2: Calculate
public static decimal Calculate(
Guid siteGuid,
Guid taxZoneGuid,
Guid taxClassGuid,
decimal taxableTotal,
int roundingDecimalPlaces,
MidpointRounding roundingMode)
{
decimal taxTotal = 0;
if (taxZoneGuid != Guid.Empty)
{
Collection<TaxRate> taxRates = TaxRate.GetTaxRates(siteGuid, taxZoneGuid);
if (taxRates.Count > 0)
{
foreach (TaxRate taxRate in taxRates)
{
if (taxClassGuid == taxRate.TaxClassGuid)
{
taxTotal += (taxRate.Rate * taxableTotal);
break;
}
}
}
}
return taxTotal;
}
示例3: Round
public static TimeSpan Round(this TimeSpan time, TimeSpan roundingInterval, MidpointRounding roundingType)
{
return new TimeSpan(
Convert.ToInt64(Math.Round(
time.Ticks / (decimal)roundingInterval.Ticks,
roundingType
)) * roundingInterval.Ticks
);
}
示例4: Round
public int Round(
double value,
int digits,
MidpointRounding mode
)
{
int result = MathExtensions.Round(value, digits, mode);
return result;
// TODO: add assertions to method MathExtensionsTest.Round(Double, Int32, MidpointRounding)
}
示例5: ConvertAmount
public static double ConvertAmount(
double amount,
CurrencyCode originalCurrencyCode,
CurrencyCode targetCurrencyCode,
int decimalDigits = 2,
MidpointRounding midPointRounding = MidpointRounding.AwayFromZero)
{
double originalExchangeRate = GetExchangeRate(originalCurrencyCode);
double targetExchangeRate = GetExchangeRate(targetCurrencyCode);
double amount_USD = amount / originalExchangeRate;
double amount_TargetCurrency = Math.Round(amount_USD * targetExchangeRate, decimalDigits, midPointRounding);
return amount_TargetCurrency;
}
示例6: SafeDivide
public static IEnumerable<Money> SafeDivide(this Money money, int shares, MidpointRounding rounding)
{
if (shares <= 1)
throw new ArgumentOutOfRangeException(nameof(shares), "Number of shares must be greater than 1");
decimal shareAmount = Math.Round(money.Amount / shares, (int)money.Currency.DecimalDigits, rounding);
decimal remainder = money.Amount;
for (int i = 0; i < shares - 1; i++)
{
remainder -= shareAmount;
yield return new Money(shareAmount, money.Currency);
}
yield return new Money(remainder, money.Currency);
}
示例7: InternalRound
[System.Security.SecuritySafeCritical] // auto-generated
private static unsafe double InternalRound(double value, int digits, MidpointRounding mode) {
if (Abs(value) < doubleRoundLimit) {
Double power10 = roundPower10Double[digits];
value *= power10;
if (mode == MidpointRounding.AwayFromZero) {
double fraction = SplitFractionDouble(&value);
if (Abs(fraction) >= 0.5d) {
value += Sign(fraction);
}
}
else {
// On X86 this can be inlined to just a few instructions
value = Round(value);
}
value /= power10;
}
return value;
}
示例8: Round
public static double Round(double value, int decimals, MidpointRounding midpointRounding)
{
var roundingMode = Java.Math.RoundingMode.UNNECESSARY;
switch (midpointRounding)
{
case MidpointRounding.AwayFromZero:
roundingMode = Java.Math.RoundingMode.HALF_UP;
break;
case MidpointRounding.ToEven:
roundingMode = Java.Math.RoundingMode.HALF_EVEN;
break;
}
var bigDecimal = new Java.Math.BigDecimal(value);
bigDecimal = bigDecimal.SetScale(decimals, roundingMode);
return bigDecimal.DoubleValue();
}
示例9: InternalRound
private unsafe static double InternalRound(double value, int digits, MidpointRounding mode)
{
if (Math.Abs(value) < Math.doubleRoundLimit)
{
double num = Math.roundPower10Double[digits];
value *= num;
if (mode == MidpointRounding.AwayFromZero)
{
double value2 = Math.SplitFractionDouble(&value);
if (Math.Abs(value2) >= 0.5)
{
value += (double)Math.Sign(value2);
}
}
else
{
value = Math.Round(value);
}
value /= num;
}
return value;
}
示例10: Euro
/// <summary>Initializes a new instance of the <see cref="Money"/> structure in euro's.</summary>
/// <param name="amount">The Amount of money in euro.</param>
/// <param name="rounding">The rounding.</param>
/// <returns>A <see cref="Money"/> structure with EUR as <see cref="Currency"/>.</returns>
public static Money Euro(double amount, MidpointRounding rounding)
{
return new Money((decimal)amount, Currency.FromCode("EUR"), rounding);
}
示例11: Round
public static Decimal Round(Decimal d, int decimals, MidpointRounding mode) {
return Decimal.Round(d, decimals, mode);
}
示例12: RoundToInt
public static int RoundToInt(float f, MidpointRounding mode = MidpointRounding.AwayFromZero)
{
return (int) Math.Round(f, mode);
}
示例13: Round
/// <summary>
/// Rounds a value to a specified number of fractional digits.
/// </summary>
/// <param name="value">The input value.</param>
/// <param name="decimals">The number of decimal places in the return value.</param>
/// <param name="mode">
/// Specification for how to round <paramref name="value" /> if it is midway between two other numbers.
/// </param>
/// <returns>The output value.</returns>
public static decimal Round(this decimal value, int decimals = 0, MidpointRounding mode = MidpointRounding.ToEven)
{
return Math.Round(value, decimals, mode);
}
示例14: Round
public static F Round(int decimals, MidpointRounding mode) => x => Math.Round(x, decimals, mode);
示例15: Yen
/// <summary>Initializes a new instance of the <see cref="Money"/> structure in Japanese Yens.</summary>
/// <param name="amount">The Amount of money in euro.</param>
/// <param name="rounding">The rounding.</param>
/// <returns>A <see cref="Money"/> structure with JPY as <see cref="Currency"/>.</returns>
public static Money Yen(decimal amount, MidpointRounding rounding)
{
return new Money(amount, Currency.FromCode("JPY"), rounding);
}