/// <summary> /// Converts a raw decimal pounds and pence to pennies /// </summary> /// <param name="rawPoundsAndPence">Raw decimal pounds and pence value</param> /// <param name="direction">Whether to round down or up to the nearest penny</param> /// <returns>The number of whole pennies in the amount</returns> public static int ToPence(decimal rawPoundsAndPence, RoundDirection direction) { decimal d = rawPoundsAndPence * 100m; if (direction == RoundDirection.Down) { return (int)Math.Truncate(d); } else { return (int)Math.Ceiling(d); } }
/// <summary> /// Rounds a number to the nearest multiple /// </summary> /// <param name="number">The number to round</param> /// <param name="multiple">The multiple to round to</param> /// <param name="direction">The direction to round</param> /// <returns>A rounded number</returns> public static int RoundToNearest(int number, int multiple, RoundDirection direction) { double d = number / (double)multiple; int i = 0; if (direction == RoundDirection.Up) { i = (int)Math.Ceiling(d); } else { i = (int)Math.Floor(d); } return multiple * i; }
void updateStartCirclePoint() { // TODO: get far point and random radius and fly height direction = Random.Range(0, 2) == 0 ? RoundDirection.CLOCK_WISE : RoundDirection.COUNTER_CLOCK_WISE; flyCircleCurrentRotation = (transform.rotation.eulerAngles.y + 270) % 360; float positionDeg = (transform.rotation.eulerAngles.y + 90) % 360; targetRotation = transform.rotation.eulerAngles.y + (direction == RoundDirection.CLOCK_WISE ? 90 : -90); flyCircleHeight = flyHeight; flyCircleRadius = new Vector3(flyRadius, 0, 0); Quaternion rotation = new Quaternion(); rotation.eulerAngles = new Vector3(0, flyCircleCurrentRotation, 0); Vector3 positionXZ = rotation * flyCircleRadius; positionXZ.y = flyCircleHeight; targetPosition = positionXZ; }
private static double RoundInchesOfDecimalFeet(double oldDecimalFeet, double roundingValue, RoundDirection roundValue) { const double unit = 12.0; const int digits = 6; var oldDecimalInches = Math.Round(oldDecimalFeet * unit, digits); var oldWholeInches = Math.Truncate(oldDecimalInches); var oldPartialInches = oldDecimalInches - oldWholeInches; var newPartialInches = 0.0; if (Math.Abs(oldPartialInches % roundingValue) <= 0) { newPartialInches = oldPartialInches; } else { if (roundValue == RoundDirection.Up) { newPartialInches = RoundLogic.RoundPartialInchUp(oldPartialInches, roundingValue); } else if (roundValue == RoundDirection.Down) { newPartialInches = RoundLogic.RoundPartialInchDown(oldPartialInches, roundingValue); } } var newDecimalInches = oldWholeInches + newPartialInches; var newDecimalFeet = newDecimalInches / unit; var oldFraction = ConvertDecimalFeetToFractionalFormat(oldDecimalFeet, roundingValue, FractionalFormat.FeetInchAndFraction); var newFraction = ConvertDecimalFeetToFractionalFormat(newDecimalFeet, roundingValue, FractionalFormat.FeetInchAndFraction); Console.WriteLine($"Old Decimal Feet: {oldDecimalFeet}\t({oldFraction}).\t\tNew Decimal Feet: {newDecimalFeet}\t({newFraction})."); return(newDecimalFeet); }
public static int Rounded(this double num, RoundDirection dir) => dir switch {
/// <summary> /// Converts a raw decimal pounds and pence amount to the nearest penny /// </summary> /// <param name="rawPoundsAndPence">Raw decimal pounds and pence value</param> /// <param name="direction">Whether to round down or up to the nearest penny</param> /// <returns>The number of whole pounds and pennies</returns> public static decimal RoundToPoundsAndPence(decimal rawPoundsAndPence, RoundDirection direction) { return ToPence(rawPoundsAndPence, direction) / 100m; }