What is it?
Numerics is a library that is loosely (and probably inaccurately) named after the package in .NET, System.Numerics
, which provides utilities for numbers in C#.
I designed this library for use in my games because there’s a lot of cases where I need to show the user some number - especially larger numbers - and need to have it fit within a certain space. There’s nothing super special about this module, so I’ve decided to open source it. Having this just laying around is nice! Saves you some work.
Unlike other modules of a similar style, this focuses mostly on the display. It contains tools to modify values, but its core focus is the display of values. If you’re looking for a module offering powerful mathematical functions put into a neat package, this might not be the right one and I encourage you to go look at some of the other ones that have been released.
You can get this module here.
Just note that there might be a few hiccups in processing, so if you get any odd output, let me know what function caused it, what arguments you used, and what kind of output you were expecting.
API
NumberTools.TruncateNumber(value, places)
This function takes in a value that is expected to be incredibly large (over 1000), and appends a letter ending based on SI number endings. For instance, passing in 14127894234789
and a places value of 1
will return 14.1T
. A notable feature of this system is that if you manage to pass in a value over 1,000,000,000,000,000,000,000,000
, it will combine endings, for instance, that value * 1,000
will return 1KY
. The endings can be customized and the amount of available endings can also be altered at your discretion via editing the script.
This is super useful for displaying stuff like healthbars, and that’s actually why I designed this function. It’s very malleable and works with just about any value.
NumberTools.FormatPercentage(value)
This function takes in a numeric value like 0.45782
and creates a % value rounded to two decimal places (45.78%
)
NumberTools.Round(value, places)
This function rounds the given value to a given amount of places, e.g. passing in 492.458657
with a places
argument of 3 will return 492.459
.
NumberTools.Clamp01(value)
Clamps the input value between 0 and 1.
NumberTools.Lerp(start, goal, alpha)
Linearly interpolates from start to goal by alpha%.
NumberTools.Map(value, inMin, inMax, outMin, outMax)
Maps a value ranging from inMin
to inMax
to a value ranging from outMin
to outMax
. For a simple example, say value
ranges from 0 to 100, and I want it to range from 0 to 500. In a case where value=50
, this function will output 250
.
NumberTools.Map01(value, min, max)
Maps the input value, which is expected to range from min to max, into a range of 0 to 1.
NumberTools.WithCommas(value, useAltCommas)
Appends commas to a number to separate the places, e.g. passing in 3829.4891
will return 3,829.4891
. If useAltCommas
is true, then it will output 3.829,4891
for the purposes of localization.
This does not put commas into the decimal portion of a value.
NumberTools.SecondsToClock(seconds, noHours)
Takes in a given number of seconds and returns a clock time. If noHours
is true, then the clock will be limited to minutes:seconds
rather than hours:minutes:seconds
.