Round to Decimals#
Math / Arithmetic / Round to Decimals
Can Read
Cannot Write
Description#
Rounds a number keeping a certain amount of decimal digits
Parameters#
| Name | Description |
|---|---|
| Number | The number that is rounded |
| Decimals | How many decimal digits are kept. Between 0 and 15 |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Round to Decimals")]
[Category("Math/Arithmetic/Round to Decimals")]
[Description("Rounds a number keeping a certain amount of decimal digits")]
[Image(typeof(IconContrast), ColorTheme.Type.Yellow, typeof(BadgeHash))]
[Parameter("Number", "The number that is rounded")]
[Parameter("Decimals", "How many decimal digits are kept. Between 0 and 15")]
[Keywords("Decimal", "Precision", "Digits", "Nearest", "Approximate")]
[Serializable]
public class TypeNumberMathRoundToDecimals : TypeNumber
{
private const int MAX_DECIMALS = 15;
[SerializeField] private GetNumber m_Number = new GetNumber();
[SerializeField] private GetInteger m_Decimals = new GetInteger(2);
public override double GetPreview(GameObject source) => Round(
this.m_Number.GetPreview(source),
(int) this.m_Decimals.GetPreview(source)
);
public override double Get(Args args) => Round(
this.m_Number.Get(args),
this.m_Decimals.GetInt(args)
);
private static double Round(double number, int decimals)
{
return Math.Round(number, Mathf.Clamp(decimals, 0, MAX_DECIMALS));
}
public override string ToString()
{
return $"Round({this.m_Number}, {this.m_Decimals})";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}