Example #1
0
        public CubeCoordinatesInt(CubeCoordinatesFloat cubeCoordinatesFloat, RoundingMethod roundingMethod)
        {
            int qInt;
            int rInt;

            switch (roundingMethod)
            {
            case RoundingMethod.Ceil:
                qInt = Mathf.CeilToInt(cubeCoordinatesFloat.Q);
                rInt = Mathf.CeilToInt(cubeCoordinatesFloat.R);
                break;

            case RoundingMethod.Floor:
                qInt = Mathf.FloorToInt(cubeCoordinatesFloat.Q);
                rInt = Mathf.FloorToInt(cubeCoordinatesFloat.R);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(roundingMethod), roundingMethod, null);
            }

            Q = qInt;
            R = rInt;
            S = -qInt - rInt;
        }
        /// <inheritdoc/>
        protected override async Task OnFirstAfterRenderAsync()
        {
            dotNetObjectRef ??= CreateDotNetObjectRef(new NumericPickerAdapter(this));

            // find the min and max possible value based on the supplied value type
            var(minFromType, maxFromType) = Converters.GetMinMaxValueOfType <TValue>();

            await JSModule.Initialize(dotNetObjectRef, ElementRef, ElementId, new
            {
                Value,
                Immediate        = IsImmediate,
                Debounce         = IsDebounce,
                DebounceInterval = DebounceIntervalValue,
                Decimals,
                DecimalSeparator,
                AlternativeDecimalSeparator,
                GroupSeparator,
                GroupSpacing,
                CurrencySymbol,
                CurrencySymbolPlacement = CurrencySymbolPlacement.ToCurrencySymbolPlacement(),
                RoundingMethod          = RoundingMethod.ToNumericRoundingMethod(),
                AllowDecimalPadding     = AllowDecimalPadding.ToNumericDecimalPadding(),
                AlwaysAllowDecimalSeparator,
                Min = MinDefined ? (object)Min : null,
                Max = MaxDefined ? (object)Max : null,
                MinMaxLimitsOverride = MinMaxLimitsOverride.ToNumericMinMaxLimitsOverride(),
                TypeMin = minFromType,
                TypeMax = maxFromType,
                Step,
                SelectAllOnFocus,
            });

            await base.OnFirstAfterRenderAsync();
        }
Example #3
0
            private static RoundingUnitAndMethod GetRoundingUnitAndMethod(RequestContext context, string currencyCode, bool useSalesRounding)
            {
                GetCurrencyByCodeDataRequest dataRequest = new GetCurrencyByCodeDataRequest(currencyCode, new ColumnSet());
                var currency = context.Execute <SingleEntityDataServiceResponse <Currency> >(dataRequest).Entity;

                decimal        roundingUnit   = Rounding.DefaultRoundingValue;
                RoundingMethod roundingMethod = RoundingMethod.Nearest;

                if (currency != null)
                {
                    roundingUnit = useSalesRounding ? currency.RoundOffSales : currency.RoundOffPrice;
                    if (roundingUnit == decimal.Zero)
                    {
                        roundingUnit = Rounding.DefaultRoundingValue;
                    }

                    int roundoffType = useSalesRounding ? currency.RoundOffTypeSales : currency.RoundOffTypePrice;
                    roundingMethod = Rounding.ConvertRoundOffTypeToRoundingMethod(roundoffType);
                }
                else
                {
                    NetTracer.Warning("No currency found for currency code {0}. Falling back to default rounding value ({1}).", currencyCode, Rounding.DefaultRoundingValue);
                }

                return(new RoundingUnitAndMethod(roundingUnit, roundingMethod));
            }
Example #4
0
        public static CubeCoordinatesInt FromUnity(Vector2 point, float size, RoundingMethod roundingMethod)
        {
            var q = (2f / 3 * point.x) / size;
            var r = (-1f / 3 * point.x + Mathf.Sqrt(3) / 3 * point.y) / size;

            int qInt;
            int rInt;

            switch (roundingMethod)
            {
            case RoundingMethod.Ceil:
                qInt = Mathf.CeilToInt(q);
                rInt = Mathf.CeilToInt(r);
                break;

            case RoundingMethod.Floor:
                qInt = Mathf.FloorToInt(q);
                rInt = Mathf.FloorToInt(r);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(roundingMethod), roundingMethod, null);
            }

            return(new CubeCoordinatesInt(qInt, rInt));
        }
Example #5
0
 public static float Round(float f, RoundingMethod roundingMethod)
 {
     if (roundingMethod == RoundingMethod.HalfOrMoreRoundsUp)
     {
         if (f % 1 >= 0.5f)
         {
             return(Mathf.Round(f));
         }
     }
     else if (roundingMethod == RoundingMethod.HalfOrLessRoundsDown)
     {
         if (f % 1 <= 0.5f)
         {
             return((int)f);
         }
     }
     else if (roundingMethod == RoundingMethod.RoundUpIfNotInteger)
     {
         if (f % 1 != 0)
         {
             return(Mathf.Round(f));
         }
     }
     else            // if (roundingMethod == RoundingMethod.RoundDownIfNotInteger)
     {
         if (f % 1 != 0)
         {
             return((int)f);
         }
     }
     return(f);
 }
Example #6
0
            /// <summary>
            /// Rounds values to nearest currency unit, i.e 16,45 kr. rounded up if the smallest coin is 10 kr will give 20 kr.
            /// or if the smallest coin is 24 aurar(0,25 kr.) then if rounded up it will give 16,50 kr.
            /// </summary>
            /// <param name="value">The currency value or value to be rounded.</param>
            /// <param name="unit">The smallest unit to be rounded to.</param>
            /// <param name="roundMethod">The method of rounding (i.e. nearest, up or down).</param>
            /// <returns>Returns a value rounded to the nearest unit.</returns>
            public static decimal RoundToUnit(decimal value, decimal unit, RoundingMethod roundMethod)
            {
                if (unit == decimal.Zero)
                {
                    unit = DefaultRoundingValue;
                }

                if (roundMethod == RoundingMethod.None)
                {
                    roundMethod = RoundingMethod.Nearest;
                }

                decimal decimalValue = value / unit;
                decimal difference   = Math.Abs(decimalValue) - Math.Abs(Math.Truncate(value / unit));

                // is rounding required?
                if (difference > 0)
                {
                    switch (roundMethod)
                    {
                    case RoundingMethod.Nearest:
                    {
                        return(Math.Round(Math.Round(value / unit, 0, MidpointRounding.AwayFromZero) * unit, GetNumberOfDecimals(unit), MidpointRounding.AwayFromZero));
                    }

                    case RoundingMethod.Down:
                    {
                        if (value > 0M)
                        {
                            return(Math.Round(Math.Round((value / unit) - 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                        }

                        return(Math.Round(Math.Round((value / unit) + 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                    }

                    case RoundingMethod.Up:
                    {
                        if (value > 0M)
                        {
                            return(Math.Round(Math.Round((value / unit) + 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                        }

                        return(Math.Round(Math.Round((value / unit) - 0.5M, 0) * unit, GetNumberOfDecimals(unit)));
                    }
                    }
                }
                else if (difference == 0M)
                {
                    // for scenarios like value == 69.9900000000, the unit == 0.01 and the difference == 0.000000000
                    return(Math.Round(value, GetNumberOfDecimals(unit)));
                }

                return(value);
            }
Example #7
0
            /// <summary>
            /// Round for channel payment method.
            /// </summary>
            /// <param name="request">Service request.</param>
            /// <returns>Rounded value.</returns>
            private GetRoundedValueServiceResponse GetPaymentRoundedValue(GetPaymentRoundedValueServiceRequest request)
            {
                GetChannelTenderTypesDataRequest       dataServiceRequest = new GetChannelTenderTypesDataRequest(request.RequestContext.GetPrincipal().ChannelId, QueryResultSettings.AllRecords);
                EntityDataServiceResponse <TenderType> response           = request.RequestContext.Execute <EntityDataServiceResponse <TenderType> >(dataServiceRequest);

                TenderType tenderType = response.PagedEntityCollection.Results.SingleOrDefault(channelTenderType => string.Equals(channelTenderType.TenderTypeId, request.TenderTypeId, StringComparison.OrdinalIgnoreCase));

                RoundingMethod roundingMethod = tenderType.RoundingMethod;

                if (roundingMethod == RoundingMethod.None)
                {
                    return(new GetRoundedValueServiceResponse(request.Value));
                }

                decimal currencyUnit = tenderType.RoundOff;

                if (currencyUnit == decimal.Zero)
                {
                    currencyUnit = Rounding.DefaultRoundingValue;
                }

                decimal roundedValue;

                if (request.IsChange)
                {
                    // For change rounding up/down should be applied in opposite direction.
                    if (roundingMethod == RoundingMethod.Down)
                    {
                        roundingMethod = RoundingMethod.Up;
                    }
                    else if (roundingMethod == RoundingMethod.Up)
                    {
                        roundingMethod = RoundingMethod.Down;
                    }
                }

                // Using absolute value so payment and refund is rounded same way when rounding up or down.
                decimal absoluteAmount = Math.Abs(request.Value);

                roundedValue = Rounding.RoundToUnit(absoluteAmount, currencyUnit, roundingMethod);
                if (request.Value < 0)
                {
                    // Revert sign back to original.
                    roundedValue = decimal.Negate(roundedValue);
                }

                return(new GetRoundedValueServiceResponse(roundedValue));
            }
Example #8
0
        public string Format(double number, RoundingMethod method, int roundingPrecision)
        {
            double precisionFactor = Math.Pow(10, roundingPrecision);

            switch (method)
            {
            case RoundingMethod.Ceil:
                number = Math.Ceiling(number * precisionFactor) / precisionFactor;
                break;

            case RoundingMethod.Floor:
                number = Math.Floor(number * precisionFactor) / precisionFactor;
                break;

            case RoundingMethod.Midpoint:
                number = Math.Round(number, roundingPrecision, MidpointRounding.AwayFromZero);
                break;
            }
            return(Format(number));
        }
    public void OnEnable()
    {
        // restore settings
        PrefsSurrogate prefs = JsonUtility.FromJson <PrefsSurrogate>(EditorPrefs.GetString(PREFS_PATH));

        snap_offset = prefs.snap_offset;
        minRotation = prefs.minRotation;
        maxRotation = prefs.maxRotation;
        roundMethod = prefs.roundMethod;

        this.positionProperty     = this.serializedObject.FindProperty("m_LocalPosition");
        this.rotationProperty     = this.serializedObject.FindProperty("m_LocalRotation");
        this.scaleProperty        = this.serializedObject.FindProperty("m_LocalScale");
        icon_revert               = EditorGUIUtility.isProSkin ? Resources.Load("uEditor_Revert_pro") as Texture2D : Resources.Load("uEditor_Revert") as Texture2D;
        icon_locked               = Resources.Load("uEditor_locked") as Texture2D;
        icon_round                = Resources.Load("uEditor_round") as Texture2D;
        icon_unlocked             = Resources.Load("uEditor_unlocked") as Texture2D;
        style_utilLabel           = uEditorUtils.uEditorSkin.GetStyle("UtilLabel");
        style_resetButton         = uEditorUtils.uEditorSkin.GetStyle("ResetButton");
        EditorApplication.update += EditorUpdate;
    }
Example #10
0
 /// <summary>
 /// Round an amount.
 /// </summary>
 /// <param name="amountToRound">Amount to round.</param>
 /// <param name="roundingOff">Precision of rounding.</param>
 /// <param name="roundingMethod">Nearest, up or down.</param>
 /// <returns>Rounded amount.</returns>
 public decimal Round(decimal amountToRound, decimal roundingOff, RoundingMethod roundingMethod)
 {
     return(Rounding.RoundToUnit(amountToRound, roundingOff, roundingMethod));
 }
    private void DrawUtilities()
    {
        GUILayout.Space(5);
        //Snap to ground
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Snap To Ground", uEditorUtils.uEditorSkin.button, GUILayout.Width(ThinInspectorMode ? 100 : 160)))
        {
            foreach (var tar in this.targets)
            {
                Transform t = (Transform)tar;
                Undo.RecordObject(t, "Snap to Ground");
                t.TransformSnapToGround(snap_offset);
            }
        }
        EditorGUIUtility.labelWidth = 50f;
        snap_offset = EditorGUILayout.FloatField("Offset", snap_offset);
        if (GUILayout.Button(new GUIContent("", icon_revert, "Reset Offset"), style_resetButton, GUILayout.Width(18), GUILayout.Height(18)))
        {
            snap_offset = 0f;
        }
        GUILayout.EndHorizontal();


        //Random rotation
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Random Rotation", uEditorUtils.uEditorSkin.button, GUILayout.Width(ThinInspectorMode ? 100 : 160), GUILayout.Height(EditorGUIUtility.singleLineHeight * 2)))
        {
            foreach (var tar in this.targets)
            {
                Transform t = (Transform)tar;
                Undo.RecordObject(t, "Random Rotation");
                t.RandomiseRotation(minRotation, maxRotation);
            }
        }

        GUILayout.BeginVertical();
        GUILayout.BeginHorizontal();
        {
            minRotation = EditorGUILayout.Vector3Field(ThinInspectorMode ? "" : "Min", minRotation);
            if (GUILayout.Button(new GUIContent("", icon_revert, "Reset Rotation Min"), style_resetButton, GUILayout.Width(18), GUILayout.Height(18)))
            {
                minRotation = Vector3.zero;
            }
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(2);

        GUILayout.BeginHorizontal();
        {
            maxRotation = EditorGUILayout.Vector3Field(ThinInspectorMode ? "" : "Max", maxRotation);
            if (GUILayout.Button(new GUIContent("", icon_revert, "Reset Rotation Max"), style_resetButton, GUILayout.Width(18), GUILayout.Height(18)))
            {
                maxRotation = new Vector3(360, 360, 360);
            }
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(5);


        GUILayout.EndVertical();



        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("Rounding Method", style_utilLabel, GUILayout.Width(ThinInspectorMode ? 100 : 160), GUILayout.Height(EditorGUIUtility.singleLineHeight));
            roundMethod = (RoundingMethod)EditorGUILayout.EnumPopup(roundMethod);
            if (GUILayout.Button(new GUIContent("", icon_revert, "Reset Rounding"), style_resetButton, GUILayout.Width(18), GUILayout.Height(18)))
            {
                roundMethod = RoundingMethod.Floor;
            }
        }
        GUILayout.EndHorizontal();
        EditorGUIUtility.labelWidth = 0;
    }
 public CubeCoordinatesInt ToInt(RoundingMethod roundingMethod)
 {
     return(new CubeCoordinatesInt(this, roundingMethod));
 }
Example #13
0
 public OffsetCoordinatesInt ToInt(RoundingMethod roundingMethod)
 {
     return(new OffsetCoordinatesInt(this, roundingMethod));
 }
Example #14
0
        /// <inheritdoc/>
        public override async Task SetParametersAsync(ParameterView parameters)
        {
            if (Rendered)
            {
                var decimalsChanged                    = isIntegerType ? false : parameters.TryGetValue <int>(nameof(Decimals), out var paramDecimals) && !Decimals.IsEqual(paramDecimals);
                var decimalSeparatorChanged            = parameters.TryGetValue <string>(nameof(DecimalSeparator), out var paramDecimalSeparator) && !DecimalSeparator.IsEqual(paramDecimalSeparator);
                var alternativeDecimalSeparatorChanged = parameters.TryGetValue <string>(nameof(AlternativeDecimalSeparator), out var paramAlternativeDecimalSeparator) && !AlternativeDecimalSeparator.IsEqual(paramAlternativeDecimalSeparator);

                var groupSeparatorChanged = parameters.TryGetValue <string>(nameof(GroupSeparator), out var paramGroupSeparator) && !GroupSeparator.IsEqual(paramGroupSeparator);
                var groupSpacingChanged   = parameters.TryGetValue <string>(nameof(GroupSpacing), out var paramGroupSpacing) && !GroupSpacing.IsEqual(paramGroupSpacing);

                var currencySymbolChanged          = parameters.TryGetValue <string>(nameof(CurrencySymbol), out var paramCurrencySymbol) && !CurrencySymbol.IsEqual(paramCurrencySymbol);
                var currencySymbolPlacementChanged = parameters.TryGetValue <CurrencySymbolPlacement>(nameof(CurrencySymbolPlacement), out var paramCurrencySymbolPlacement) && !CurrencySymbolPlacement.IsEqual(paramCurrencySymbolPlacement);

                var roundingMethodChanged = parameters.TryGetValue <NumericRoundingMethod>(nameof(RoundingMethod), out var paramRoundingMethod) && !RoundingMethod.IsEqual(paramRoundingMethod);

                var minChanged = parameters.TryGetValue <TValue>(nameof(Min), out var paramMin) && !Min.IsEqual(paramMin);
                var maxChanged = parameters.TryGetValue <TValue>(nameof(Max), out var paramMax) && !Max.IsEqual(paramMax);
                var minMaxLimitsOverrideChanged = parameters.TryGetValue <NumericMinMaxLimitsOverride>(nameof(MinMaxLimitsOverride), out var paramMinMaxLimitsOverride) && !MinMaxLimitsOverride.IsEqual(paramMinMaxLimitsOverride);

                var selectAllOnFocusChanged = parameters.TryGetValue <bool>(nameof(SelectAllOnFocus), out var paramSelectAllOnFocus) && !SelectAllOnFocus.IsEqual(paramSelectAllOnFocus);

                var allowDecimalPaddingChanged         = parameters.TryGetValue <NumericAllowDecimalPadding>(nameof(AllowDecimalPadding), out var paramAllowDecimalPadding) && !AllowDecimalPadding.IsEqual(paramAllowDecimalPadding);
                var alwaysAllowDecimalSeparatorChanged = parameters.TryGetValue <bool>(nameof(AlwaysAllowDecimalSeparator), out var paramAlwaysAllowDecimalSeparator) && !AlwaysAllowDecimalSeparator.IsEqual(paramAlwaysAllowDecimalSeparator);

                var modifyValueOnWheelChanged = parameters.TryGetValue <bool>(nameof(ModifyValueOnWheel), out var paramModifyValueOnWheel) && !ModifyValueOnWheel.IsEqual(paramModifyValueOnWheel);
                var wheelOnChanged            = parameters.TryGetValue <NumericWheelOn>(nameof(WheelOn), out var paramWheelOn) && !WheelOn.IsEqual(paramWheelOn);

                if (decimalsChanged || decimalSeparatorChanged || alternativeDecimalSeparatorChanged ||
                    groupSeparatorChanged || groupSpacingChanged ||
                    currencySymbolChanged || currencySymbolPlacementChanged ||
                    roundingMethodChanged ||
                    minChanged || maxChanged ||
                    selectAllOnFocusChanged ||
                    allowDecimalPaddingChanged || alwaysAllowDecimalSeparatorChanged ||
                    modifyValueOnWheelChanged)
                {
                    ExecuteAfterRender(async() => await JSModule.UpdateOptions(ElementRef, ElementId, new
                    {
                        Decimals                    = new { Changed = decimalsChanged, Value = GetDecimals() },
                        DecimalSeparator            = new { Changed = decimalSeparatorChanged, Value = paramDecimalSeparator },
                        AlternativeDecimalSeparator = new { Changed = alternativeDecimalSeparatorChanged, Value = paramAlternativeDecimalSeparator },
                        GroupSeparator              = new { Changed = groupSeparatorChanged, Value = paramGroupSeparator },
                        GroupSpacing                = new { Changed = groupSpacingChanged, Value = paramGroupSpacing },
                        CurrencySymbol              = new { Changed = currencySymbolChanged, Value = paramCurrencySymbol },
                        CurrencySymbolPlacement     = new { Changed = currencySymbolPlacementChanged, Value = paramCurrencySymbolPlacement.ToCurrencySymbolPlacement() },
                        RoundingMethod              = new { Changed = roundingMethodChanged, Value = paramRoundingMethod.ToNumericRoundingMethod() },
                        AllowDecimalPadding         = new { Changed = allowDecimalPaddingChanged, Value = paramAllowDecimalPadding.ToNumericDecimalPadding() },
                        AlwaysAllowDecimalSeparator = new { Changed = alwaysAllowDecimalSeparatorChanged, Value = paramAlwaysAllowDecimalSeparator },
                        Min = new { Changed = minChanged, Value = paramMin },
                        Max = new { Changed = maxChanged, Value = paramMax },
                        MinMaxLimitsOverride = new { Changed = minMaxLimitsOverrideChanged, Value = paramMinMaxLimitsOverride },
                        SelectAllOnFocus     = new { Changed = selectAllOnFocusChanged, Value = paramSelectAllOnFocus },
                        ModifyValueOnWheel   = new { Changed = modifyValueOnWheelChanged, Value = paramModifyValueOnWheel },
                        WheelOn = new { Changed = wheelOnChanged, Value = paramWheelOn },
                    }));
                }

                var valueChanged = parameters.TryGetValue <TValue>(nameof(Value), out var paramValue) && !Value.IsEqual(paramValue);

                if (valueChanged)
                {
                    ExecuteAfterRender(async() => await JSModule.UpdateValue(ElementRef, ElementId, paramValue));
                }
            }

            // This make sure we know that Min or Max parameters are defined and can be checked against the current value.
            // Without we cannot determine if Min or Max has a default value when TValue is non-nullable type.
            MinDefined = parameters.TryGetValue <TValue>(nameof(Min), out var min);
            MaxDefined = parameters.TryGetValue <TValue>(nameof(Max), out var max);

            await base.SetParametersAsync(parameters);

            if (ParentValidation != null)
            {
                if (parameters.TryGetValue <Expression <Func <TValue> > >(nameof(ValueExpression), out var expression))
                {
                    await ParentValidation.InitializeInputExpression(expression);
                }

                if (parameters.TryGetValue <string>(nameof(Pattern), out var pattern))
                {
                    // make sure we get the newest value
                    var value = parameters.TryGetValue <TValue>(nameof(Value), out var inValue)
                        ? inValue
                        : InternalValue;

                    await ParentValidation.InitializeInputPattern(pattern, value);
                }

                await InitializeValidation();
            }
        }
Example #15
0
 public static OffsetCoordinatesInt FromUnity(Vector2 point, float size, RoundingMethod roundingMethod)
 {
     return(CubeCoordinatesInt.FromUnity(point, size, roundingMethod).ToOffset());
 }
Example #16
0
 internal RoundingUnitAndMethod(decimal roundingUnit, RoundingMethod roundingMethod)
 {
     this.RoundingUnit   = roundingUnit;
     this.RoundingMethod = roundingMethod;
 }
Example #17
0
 public OffsetCoordinatesInt(OffsetCoordinatesFloat offsetCoordinatesFloat, RoundingMethod roundingMethod)
 {
     this = offsetCoordinatesFloat.ToCube().ToInt(roundingMethod).ToOffset();
 }
Example #18
0
 public static decimal Rounding(decimal d, RoundingMethod roundingMethod)
 {
     switch (roundingMethod)
     {
         case RoundingMethod.Disabled:
             return d;
         case RoundingMethod.Dollars:
             return Rounding(d, RoundingTypes.Ceiling, 0);
         case RoundingMethod.Ten_Dollars:
             return Rounding(d, RoundingTypes.Ceiling, 1);
         case RoundingMethod.Ten_Cent:
             return Rounding(d, RoundingTypes.Ceiling, -1);
         default:
             return d;
     }
 }
Example #19
0
 public CubeCoordinatesInt CubeFromUnity(Vector2 point, RoundingMethod roundingMethod)
 {
     return(CubeCoordinatesInt.FromUnity(point, _size, roundingMethod));
 }
Example #20
0
 public OffsetCoordinatesInt OffsetFromUnity(Vector2 point, RoundingMethod roundingMethod)
 {
     return(OffsetCoordinatesInt.FromUnity(point, _size, roundingMethod));
 }