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
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
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.
I wrote this for you.
string onem = intToSimple(1000000);
string onek = intToSimple(1000);
private string intToSimple(int number)
{
if(val > 1000000000000)
return (val / 1000000000000).ToString("0.00") + "tr";
else if(val > 1000000000)
return (val / 1000000000).ToString("0.00") + "b";
else if(val > 1000000)
return (val / 1000000).ToString("0.00") + "m";
else if(val > 1000)
return (val / 1000).ToString("0.00") + "k";
else
return value.ToString("0.00");
}
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";
}
}
You can also try the following:
Convert your number into a string in scientific format: 2400 -> 2.4e+003; 2,6000,000 -> 2.6e+006
Then replace the exponent with the desired SI-prefix (e.g. e+003 -> k, e+006 -> M, e-009 -> n)
I've created the following extension for this purpose:
using System;
using System.Globalization;
/// <summary>
/// Si prefixed string conversion class
/// </summary>
public static class SIPrefixedString
{
/// <summary>
/// converts the value into a string with SI prefix
/// </summary>
/// <param name="value">The value.</param>
/// <returns>si prefixed string</returns>
public static string ToSIPrefixedString(this double value)
{
string stringValue = value.ToString("#E+00", CultureInfo.InvariantCulture);
string[] stringValueParts = stringValue.Split("E".ToCharArray());
int mantissa = Convert.ToInt32(stringValueParts[0], CultureInfo.InvariantCulture);
int exponent = Convert.ToInt32(stringValueParts[1], CultureInfo.InvariantCulture);
while (exponent % 3 != 0)
{
mantissa *= 10;
exponent -= 1;
}
string prefixedValue = mantissa.ToString(CultureInfo.InvariantCulture);
switch (exponent)
{
case 24:
prefixedValue += "Y";
break;
case 21:
prefixedValue += "Z";
break;
case 18:
prefixedValue += "E";
break;
case 15:
prefixedValue += "P";
break;
case 12:
prefixedValue += "T";
break;
case 9:
prefixedValue += "G";
break;
case 6:
prefixedValue += "M";
break;
case 3:
prefixedValue += "k";
break;
case 0:
break;
case -3:
prefixedValue += "m";
break;
case -6:
prefixedValue += "u";
break;
case -9:
prefixedValue += "n";
break;
case -12:
prefixedValue += "p";
break;
case -15:
prefixedValue += "f";
break;
case -18:
prefixedValue += "a";
break;
case -21:
prefixedValue += "z";
break;
case -24:
prefixedValue += "y";
break;
default:
prefixedValue = "invalid";
break;
}
return prefixedValue;
}
/// <summary>
/// returns the double value for the si prefixed string
/// </summary>
/// <param name="prefixedValue">The prefixed value.</param>
/// <returns>double value</returns>
public static double FromSIPrefixedString(this string prefixedValue)
{
string scientificNotationValue = prefixedValue;
if (scientificNotationValue.Contains("E+") == false && scientificNotationValue.Contains("E-") == false)
{
scientificNotationValue = scientificNotationValue
.Replace("Y", "E+24")
.Replace("Z", "E+21")
.Replace("E", "E+18")
.Replace("P", "E+15")
.Replace("T", "E+12")
.Replace("G", "E+09")
.Replace("M", "E+06")
.Replace("k", "E+03")
.Replace("m", "E-03")
.Replace("u", "E-06")
.Replace("n", "E-09")
.Replace("p", "E-12")
.Replace("f", "E-15")
.Replace("a", "E-18")
.Replace("z", "E-21")
.Replace("y", "E-24");
}
return Convert.ToDouble(scientificNotationValue, CultureInfo.InvariantCulture);
}
this is correct one in extention form.
public static string ToSimpleK(this int val)
{
if (val > 1000000000)
return ((decimal)val / 1000000000).ToString("0.00") + "b";
else if (val > 1000000)
return ((decimal)val / 1000000).ToString("0.00") + "m";
else if (val > 1000)
return ((decimal)val / 1000).ToString("0.00") + "k";
else
return val.ToString();
}
You won't be able to do this using String.Format but you can try this:
int i = 2400;
string j = i/1000.0 + "k";
Console.WriteLine(j);
I think your question is general, anyway I post you a simple answer for some case:
private string Cnv(int num)
{
double DIV=1000f;
double f = num;
if (f<DIV) return num.ToString();
f = num / DIV;
if (f < DIV) return f.ToString("0.0k");
f = num / DIV;
if (f < DIV) return f.ToString("0.0m");
return (f / DIV).ToString("0.0g");
}