Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format to get "2.4k" from "2400"

Is there a way to use String.Format to convert numbers to number/character representations.

For example

2400 -> 2.4k
2,600,000 -> 2.6m
like image 414
Ian Vink Avatar asked Apr 11 '11 05:04

Ian Vink


2 Answers

I am providing a sample code. Please try to make it in your way

private static string ToEngineeringNotation(this double d)
    {
        double exponent = Math.Log10(Math.Abs(d));
        if (Math.Abs(d) >= 1)
        {
            switch ((int)Math.Floor(exponent))
            {
                case 0: case 1: case 2:
                    return d.ToString();
                case 3: case 4: case 5:
                    return (d / 1e3).ToString() + "k";
                case 6: case 7: case 8:
                    return (d / 1e6).ToString() + "M";
                case 9: case 10: case 11:
                    return (d / 1e9).ToString() + "G";
                case 12: case 13: case 14:
                    return (d / 1e12).ToString() + "T";
                case 15: case 16: case 17:
                    return (d / 1e15).ToString() + "P";
                case 18: case 19: case 20:
                    return (d / 1e18).ToString() + "E";
                case 21: case 22: case 23:
                    return (d / 1e21).ToString() + "Z";
                default:
                    return (d / 1e24).ToString() + "Y";
            }
        }
        else if (Math.Abs(d) > 0)
        {
            switch ((int)Math.Floor(exponent))
            {
                case -1: case -2: case -3:
                    return (d * 1e3).ToString() + "m";
                case -4: case -5: case -6:
                    return (d * 1e6).ToString() + "μ";
                case -7: case -8: case -9:
                    return (d * 1e9).ToString() + "n";
                case -10: case -11: case -12:
                    return (d * 1e12).ToString() + "p";
                case -13: case -14: case -15:
                    return (d * 1e15).ToString() + "f";
                case -16: case -17: case -18:
                    return (d * 1e18).ToString() + "a";
                case -19: case -20: case -21:
                    return (d * 1e21).ToString() + "z";
                default:
                    return (d * 1e24).ToString() + "y";
            }
        }
        else
        {
            return "0";
        }
    }
like image 194
Marshal Avatar answered Nov 08 '22 21:11

Marshal


No, I don't believe there's a format string that will do that for you.

You may well find third party libraries to do it, and I know there are built-in Win32 routines to convert a file size to a representation like that, but that may well use 1024 rather than 1000 for the "base" of the K/M/etc. This Stack Overflow answer shows some C# code for it, but I'm sure there's something in the platform too... and as I say, that's aimed at file sizes, which may or may not be what you want.

like image 5
Jon Skeet Avatar answered Nov 08 '22 21:11

Jon Skeet