Solved

Converting 1000 to 1K

Posted on 2008-06-23
10
2,167 Views
Last Modified: 2013-12-16
Experts,

Any quick ideas how can i convert above 1000 values to number + K. 1000 to 1K or 10000 to 10K and so on ?
0
Comment
Question by:dotnetpro
10 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21846808
How would you want numbers like 1234 to look?  Do you need to convert 1000000 to 1M?  1000000000 to 1B?
0
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 65 total points
ID: 21846812
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
 
LVL 3

Expert Comment

by:darkmooink
ID: 21846819
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21846896
what about with megas?
0
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 60 total points
ID: 21846973
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21847019
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
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21847061
What comes next... http://en.wikipedia.org/wiki/SI_prefix

Regards,
Bruce
0
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21847074
and if you can't get enough of big (and small) numbers: http://en.wikipedia.org/wiki/Non-SI_unit_prefix

Regards,
Bruce
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21847099
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
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21847767
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now