> For the complete documentation index, see [llms.txt](https://heroicsolo.gitbook.io/heroic-engine/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heroicsolo.gitbook.io/heroic-engine/basics/engine-utilities/mathhelper.md).

# MathHelper

This static class provides pretty wide range of additional math functionality.

### Available methods

```csharp
public static string ToRoundedString(this float _value, int _digits = 1)
```

*This extension method creates string from given float number, rounded to certain count of digits after dot symbol.*

*Example:*

```csharp
Debug.Log($"First 3 digits of PI: {Mathf.PI.ToRoundedString(2)});

// Output: 3.14
```

***

```csharp
public static string ToShortenedNumber(this int number)
```

*This extension method returns shortened representation of given integer number. For example, 10.000 will be converted to "10k" and 1.500.000 will be converted to "1,5M".*

*Example:*

```csharp
Debug.Log(1000.ToShortenedNumber());
Debug.Log(1500000.ToShortenedNumber());

// Output:
// 1k
// 1,5M
```

***

```csharp
public static void AddUnique<T>(this IList<T> self, IEnumerable<T> items)
```

*This extension method adds unique elements from given **items** collection to given list. It adds all elements which are not yet presented in this list.*

*Example:*

```csharp
List<int> list1 = new List<int> { 1, 3, 5 };
List<int> list2 = new List<int> { 1, 3, 8, 0 };

list1.AddUnique(list2);

string results = "";
foreach (int i in list1)
{
    results += i.ToString() + ", ";
}
Debug.Log(results);

// Output:
// 1, 3, 5, 8, 0,
```

***

```csharp
public static void Shuffle<T>(this IList<T> list)
```

*This extension method randomly shuffles given **list**.*

*Example:*

```csharp
List<int> list = new List<int> { 1, 2, 3, 4 };

list.Shuffle();
```

***

```csharp
public static void Shuffle<T>(this List<T> list)
```

*It does the same as previous one.*

***

```csharp
public static void SortByDistance<T>(this List<T> list, Vector3 from) where T : Transform
```

*This extension method sorts given **list** of Transforms by distance from given point, from closest one to farthest one.*

{% hint style="info" %}
This method could be useful if you need to find closest enemy or closest pickup to a certain game character.
{% endhint %}

***

```csharp
public static T GetRandomElement<T>(this List<T> list)
```

*This extension method returns random item from given **list**. If list is empty, it returns **default** value of given type.*

*Example:*

```csharp
// Code of imaginary turn based card game :)
// Getting random card from deck
Card nextCard = cardsDeck.GetRandomElement();

UseCard(nextCard);
```

***

```csharp
public static T GetRandomElementExceptOne<T>(this List<T> list, T exceptedOne)
```

*This extension method returns random item from given **list**, except certain item. If it cannot find such item, it returns* exceptedOne *item instead.*

Example:

```csharp
List<int> testList = new List<int>{0, 5, 13, 42, 50};

int randNumExcept42 = testList.GetRandomElementExceptOne(42);
// This will return 0, 5, 13 or 50
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heroicsolo.gitbook.io/heroic-engine/basics/engine-utilities/mathhelper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
