Asked by:
Using "K" on numbers higher than 3 length
Question

Hi,
I have a int in a range from 0 ~ 999,999 at a maximum.
I want to convert the numbers after the thousand to short it as K,and only when the int is higher than 9999 (10K), so numbers less than 10K would be like: 9999, 7000, etc.
/// example int NumberWithoutKilo = 10000; //random int from 0 ~ 999,999, for example: 10000 // after convert, store it into another int int NumberWithKilo = 10;
Any Ideas?
Thanks.
All replies

Divide by 1000 and put a K on the end. (untested code)
public static string ToKilo ( int value ) { if (value < 10000) return value.ToString(); return (value / 10000).ToString() + "K"; }
Of course to make this more useful you should probably consider passing in the min value to convert. You should also consider how you'd want to display some values because 12345 should probably be displayed as 12.345K.
If you're feeling particularly ambitious then create a custom formatter to do it and then you can just use ToString directly.
Michael Taylor http://www.michaeltaylorp3.net
 Proposed as answer by The Code Breaker Friday, March 9, 2018 12:59 AM

I am not sure if you want an int as result or a string as result. Using the sample code you provided the following is a way to do it.
int NumberWithoutKilo = 10000; int NumberWithKilo = (NumberWithoutKilo + 500) / 1000;
Note that that rounds up so if the value is closer to the next K then it will use the next k, so 11 will be the result from 10500.
Sam Hobbs
SimpleSamples.Info 
Divide by 1000 and put a K on the end. (untested code)
public static string ToKilo ( int value ) { if (value < 10000) return value.ToString(); return (value / 10000).ToString() + "K"; }
Of course to make this more useful you should probably consider passing in the min value to convert. You should also consider how you'd want to display some values because 12345 should probably be displayed as 12.345K.
If you're feeling particularly ambitious then create a custom formatter to do it and then you can just use ToString directly.
This might not work correctly,
See, if you divide (for example 50000) on 10000, seems to give the result: "5K"
Same as "11111", outputs "1K", so we can't just divide the number on 10.000, specially we have a range of numbers from 0 ~ 999999 (999K)some examples to clear the idea:
15000 = 15K
15975 = 15K
99999 = 99K
106920 = 106K ... etc. 


I thought you wanted 10,000 = 1K. If 1,000 = 1K then change the division to / 1000.0.
15,000 = 15 * 1,000 = 15K
150,000 = 150 * 1,000 = 150K
The algorithm is the same. Just change the value to get what you want.
Michael Taylor http://www.michaeltaylorp3.net

I thought you wanted 10,000 = 1K. If 1,000 = 1K then change the division to / 1000.0.
15,000 = 15 * 1,000 = 15K
150,000 = 150 * 1,000 = 150K
The algorithm is the same. Just change the value to get what you want.
The problem is, any value other than 0 will be counted by dividing and ruin the int
results:
15000 = 15K (works normally)
15999 = 15.999K (should be 15K only)
10001 = 10.001K and so on ..
and I apologize for being bad at math.
We can parse the first two digits of the int manually instead of math, in case this is getting us nowhere. 

When the integer exceeds or equal the value of 10,000 get the first two digit and add K to it.
Already solved it using digits,
public static string ToKilo(int value) { if (value < 10000) return value.ToString(); int digit1 = int.Parse(value.ToString()[0].ToString()); int digit2 = int.Parse(value.ToString()[1].ToString()); int digit3 = int.Parse(value.ToString()[2].ToString()); if (value > 99999) { return digit1.ToString() + digit2.ToString() + digit3.ToString() + "K"; } return digit1.ToString() + digit2.ToString() + "K"; }
and I said that in my last reply, but I'm interested in "CoolDadTx" Method.
 Edited by Zuher Laith Friday, March 9, 2018 12:28 AM

When the integer exceeds or equal the value of 10,000 get the first two digit and add K to it.
Already solved it using digits,
public static string ToKilo(int value) { if (value < 10000) return value.ToString(); int digit1 = int.Parse(value.ToString()[0].ToString()); int digit2 = int.Parse(value.ToString()[1].ToString()); int digit3 = int.Parse(value.ToString()[2].ToString()); if (value > 99999) { return digit1.ToString() + digit2.ToString() + digit3.ToString() + "K"; } return digit1.ToString() + digit2.ToString() + "K"; }
but I'm interested in "CoolDadTx" Method.
ok if you just want like that, why not try this code from the above CoolDadTx first reply
public static string ToKilo(int value) { if (value < 10000) return value.ToString(); else return $"{(value / 1000).ToString()}K"; }
 Edited by The Code Breaker Friday, March 9, 2018 12:54 AM

Ok I'm really confused then. You said this in an earlier post.
"See, if you divide (for example 50000) on 10000, seems to give the result: "5K"
Same as "11111", outputs "1K", so we can't just divide the number on 10.000, "So do you want fractional parts or not? If not then go back to integer division.
15000 / 1000 = 15K
15999 / 1000 = 15K
10001 / 1000 = 10K
"We can parse the first two digits of the int manually instead of math, in case this is getting us nowhere."
That would work for the first 99,999 values but once you get to 100,000+ you are no longer getting the correct value, from what I understand.
110,000 = 11K instead of 110K
So let's skip the implementation and just go for some examples.
24,5000 = ___K
51,750 = ___K
99,999 = ___K
199,500 = ___K
543,210 = ___K
999,999 = ___K
Michael Taylor http://www.michaeltaylorp3.net

15999 = 15.999K (should be 15K only)
Sam Hobbs
SimpleSamples.Info Edited by Simple Samples Friday, March 9, 2018 1:22 AM

So let's skip the implementation and just go for some examples.
24,5000 = ___K
51,750 = ___K
99,999 = ___K
199,500 = ___K
543,210 = ___K
999,999 = ___K
24,5000 = //the int originally will not contain a number of thousands after the comma you added (5000), means:
245000 = 245K
51,750 = 51K
99,999 = 99K
199,500 = 199K
543,210 = 543K
999,999 = 999K
Just the first two digits, if higher than (99,999) they are three digits,
I am sorry if I was the cause of any confusion about this. 
So division by 1000 should give you what you want. Reverting back to the int division (to get rid of everything to the right) puts us back to the code posted originally (adjusted for 1000).
public static string ToKilo ( int value ) { if (value < 1000) return value.ToString(); return (value / 1000).ToString() + "K"; }
Try this function against your sample inputs and see if you get the values you wanted. If not then please post the inputs that aren't working along with the expected value.Michael Taylor http://www.michaeltaylorp3.net