Converting 1000 to 1K

Experts,

Any quick ideas how can i convert above 1000 values to number + K. 1000 to 1K or 10000 to 10K and so on ?
LVL 1
dotnetproAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Bruce_1975Connect With a Mentor Commented:
One possible solution:

Regards,
Bruce
public string ConvertNumber(int Number)
{
    if (Number >= 1000)
        return string.Concat(Number / 1000, "k");
    else
        return Number.ToString();
}

Open in new window

0
 
Joel CoehoornDirector of Information TechnologyCommented:
How would you want numbers like 1234 to look?  Do you need to convert 1000000 to 1M?  1000000000 to 1B?
0
 
darkmooinkCommented:
i dont know how to do it in c# but here is the methodology
if n > 1000 then x=n/1000
result = x & "k"
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
what about with megas?
0
 
Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
This should scale a little farther:

string GetKNotation(long value)
{
    string[] suffix = {"", "K","M", "B", "T"}; // what's after T?
    int power = 3;
    while (Math.Pow(10, power) < value)
        power++;
    power--; //was one greater than what we needed
 
    return (value / (long)Math.Pow(10, power)).ToString() + suffix[power/3];
}

Open in new window

0
 
Joel CoehoornDirector of Information TechnologyCommented:
Arg, got ahead of myself.  In trying to optimize the power variable to skip a few iterations I broke it for very small numbers (<100).  This is better:
string GetKNotation(long value)
{
    string[] suffix = {"", "K","M", "B", "T"}; // what's after T?
    int power = 1;
    while (Math.Pow(10, power) < value)
        power++;
    power--; //was one greater than what we needed
 
    return (value / (long)Math.Pow(10, power)).ToString() + suffix[power/3];
}

Open in new window

0
 
Bruce_1975Commented:
What comes next... http://en.wikipedia.org/wiki/SI_prefix

Regards,
Bruce
0
 
Bruce_1975Commented:
and if you can't get enough of big (and small) numbers: http://en.wikipedia.org/wiki/Non-SI_unit_prefix

Regards,
Bruce
0
 
Joel CoehoornDirector of Information TechnologyCommented:
Hehe, yeah I can find that page.  But if I followed that guide I would have used a G for Billion (Giga)  instead of a B.  The next up is either P for Peta or Q for Quadrillion, so there's still some confusion.  Since I used a B for Billion I should probably choose a Q, but then what would you use for Quintillion?
0
 
Bruce_1975Commented:
Hi jcoehoorn,
there was a little mistake in your code. I fixed it and added the missing extensions.

Regards,
Bruce
string GetKNotation(long value)
{
    string[] suffix = {"", "k","M", "G", "T" , "P", "E", "Z", "Y"}; // what's after Y?
    int power = 0;
    while (Math.Pow(10, power) < value)
        power += 3;
    power -= 3; //was one greater than what we needed
 
    return (value / (long)Math.Pow(10, power)).ToString() + suffix[power/3];
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.