Solved

Converting 1000 to 1K

Posted on 2008-06-23
2,401 Views
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
Question by:dotnetpro

LVL 18

Expert Comment

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

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();
}
``````
0

LVL 3

Expert Comment

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

ID: 21846896
0

LVL 18

Assisted Solution

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];
}
``````
0

LVL 18

Expert Comment

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];
}
``````
0

LVL 6

Expert Comment

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

Regards,
Bruce
0

LVL 6

Expert Comment

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

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

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];
}
``````
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signaturesâ€¦
Introduction to Processes