# How to shorten number in C# unity

What im wanting to know is like, say i have the number 1000, i dont want it to show 1000 i want it to show 1K, or i have 1000000, i want it to show 1M. Any help, please!

You have to do this yourself in c#

Perhaps something like this:

``````public string ChangeNumber(decimal amount)
{
string value;
if (amount >= 1000000)
value = Math.Floor(amount / 1000000).ToString() + "M";
else if (amount >= 1000)
value = Math.Floor(amount / 1000).ToString() + "K";
else
value = amount.ToString();

return value;
}
``````

Call it like `string value = ChangeNumber(122222);`

If you donâ€™t want to truncate, but round, use round instead of floor.

``````private static readonly SortedDictionary<int, string> abbrevations = new SortedDictionary<int, string>
{
{ 1000, "k" },
{ 1000000, "m" },
{ 1000000000, "b" }
};

public static string ShortFormat<T>(this T number) where T : struct, IComparable<T>, IConvertible
{
// Check if the type is numeric
if (typeof(T).IsNumericType() == false)
{
// Throw an exception if the type is not numeric
throw new ArgumentException("Unsupported type. Only numeric types are supported.");
}

// Convert the numeric value to double
var doubleValue = Convert.ToDouble(number);

// Iterate over abbreviations in descending order
foreach (var (divisor, abbreviation) in abbrevations.OrderByDescending(pair => pair.Key))
{
// Check if the absolute value of the double value is greater than or equal to the divisor
if (!(Math.Abs(doubleValue) >= divisor)) continue;

// Calculate the rounded number and format it with the abbreviation
var roundedNumber = doubleValue / divisor;
return roundedNumber.ToString("0.##") + abbreviation;
}

// If no abbreviation is applied, return the original double value formatted
return doubleValue.ToString("0.##");
}

// Extension method to check if a Type is numeric
private static bool IsNumericType(this Type type)
{
return Type.GetTypeCode(type) switch
{
TypeCode.Byte => true,
TypeCode.SByte => true,
TypeCode.UInt16 => true,
TypeCode.Int16 => true,
TypeCode.UInt32 => true,
TypeCode.Int32 => true,
TypeCode.UInt64 => true,
TypeCode.Int64 => true,
TypeCode.Single => true,
TypeCode.Double => true,
TypeCode.Decimal => true,
_ => false
};
}
``````