1

I want to be able to have very long integers to be converted to a readable string

This goes from 1500 (1.50k) to 12.500.000.000 being 12.50b and so on to unreadable numbers that will be labeled as A then B etc.

Now I'm pretty sure there's a nifty piece of logic that says if the string length of the integer is 9 the dots should be on place 3 and 6 or something.

My math(and its programmable logic) is non-existent for me so can anyone help me out?

This is my messy and non-reusable attempt now:

int val = 150050004;
string theValue = val.ToString();
int valLength = theValue.Length;

string newVal = theValue;
if (valLength == 7) {
    newVal = theValue.Substring(0, 1) +"."+ theValue.Substring(1, 2);
}

if (valLength == 8) {
    newVal = theValue.Substring(0, 2) +"."+ theValue.Substring(2, 2);
}

if (valLength == 9) {
    newVal = theValue.Substring(0, 3) +"."+ theValue.Substring(3, 2);
}

if (valLength > 6 && valLength < 10) {
    newVal = newVal + "m";
}

Where newVal outputs 150.05m

CaptainCarl
  • 3,411
  • 6
  • 38
  • 71

1 Answers1

1

Try this to break with "." :

NumberFormatInfo numFormat = new NumberFormatInfo();
numFormat.NumberDecimalSeparator = ",";
numFormat.NumberGroupSeparator = ".";

long val = 12345678912345;
String result = val.ToString("#,##0",numFormat);

For adding suffixed info, do this :

String result = null;
if (val / 1000000000 > 1)
    result = val.ToString("#,##0,#,,B",numFormat);
else if (val / 1000000 > 1)
    result = val.ToString("#,##0,#,M", numFormat);
else if (val / 1000 > 1)
    result = val.ToString("#,##0,#K", numFormat);
else
    result = val.ToString("#,##0", numFormat);
rducom
  • 7,072
  • 1
  • 25
  • 39