---
tags:
    - "Decimal"
    - "Precision"
    - "Digits"
    - "Nearest"
    - "Approximate"
---

# Round to Decimals

[Math](../index.md) / [Arithmetic](index.md) / **Round to Decimals**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## 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

```cs
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;
    }
}

```

