none
Using "K" on numbers higher than 3 length RRS feed

  • 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.

    Thursday, March 8, 2018 2:45 PM

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

    Thursday, March 8, 2018 3:00 PM
    Moderator
  • 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

    Thursday, March 8, 2018 7:39 PM
  • 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.

    Thursday, March 8, 2018 9:03 PM
  • Hence untested code. Simple int division issue. If you want the fractional part then change the 10000 to 10000.0 and the division becomes fractional.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, March 8, 2018 9:08 PM
    Moderator
  • Hence untested code. Simple int division issue. If you want the fractional part then change the 10000 to 10000.0 and the division becomes fractional.
    Seems to be right,
    Almost perfect but, tested results:
    15000 = 1.5K
    150000 = 15K
    Thursday, March 8, 2018 9:29 PM
  • 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

    Thursday, March 8, 2018 10:15 PM
    Moderator
  • 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.

    Thursday, March 8, 2018 11:18 PM
  • When the integer exceeds or equal the value of 10,000 get the first two digit and add K to it.

    Friday, March 9, 2018 12:09 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";
    }

    and I said that in my last reply, but I'm interested in "CoolDadTx" Method.


    Friday, March 9, 2018 12:26 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";
            }




    Friday, March 9, 2018 12:39 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

    Friday, March 9, 2018 12:53 AM
    Moderator
  • 15999 = 15.999K (should be 15K only)

    If that is what your requirements are then you are the authority but it is very common to round off values so most people would say that 15999 is 16K. You know how stores like to make the price something like $159.99? They think they are tricking us into thinking the cost is less but most people think that $159.99 is $160. So it depends on requirements whether 15999 is 16K or 15K.


    Sam Hobbs
    SimpleSamples.Info


    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.

    Friday, March 9, 2018 1:30 AM
  • 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

    Friday, March 9, 2018 3:41 AM
    Moderator