- Home /
Rounding 235000 to 235?
Hello everyone, I'm looking for some assistance today with an idea I had, So in my game you have currency which you can view in the inventory. What I would like to achieve is say the player has 235145 Gold, I would like to convert that into say 235k.
Does anyone know a way to round to the first number or first 3 numbers?? and this wouldn't be to convert the whole number but only while displaying it with GUI.label.
Example:
100000 = 100k, 1000000 = 1m, 10000000 = 10m, 1000000000 = 1b,
Any assistance would be appreciated :) thank you very much!
Answer by MakeCodeNow · Sep 17, 2014 at 05:26 AM
Google and Stack Overflow know everything.
In this case it was the top search result on Google for "c# string format million"
Answer by morbidcamel · Sep 17, 2014 at 05:57 AM
Heres a nifty extension method I wrote:
public static class MathExtensions
{
public enum RoundingMode
{
Nearest,
Up,
Down
}
public static float Round(float value, int digits = 0, RoundingMode mode = RoundingMode.Nearest)
{
float rounder = digits == 0 ? 1 : (float)Math.Pow(10, (float)digits);
switch(mode)
{
case RoundingMode.Up:
return (float) (Math.Ceiling(value/rounder) ) * rounder;
case RoundingMode.Down:
return (float) (Math.Floor(value/rounder) ) * rounder;
default:
return (float) (Math.Round(value/rounder) ) * rounder;
}
}
}