0

I have the following :

value = 15123;
value.ToString("#,0K", System.Globalization.CultureInfo.CurrentUICulture);

The output of this is : 15.199K

But I want it to output the following : 15.1K

I sincerely don't understand how to get it. I am trying to accomplish the same number format that stackoverflow has, regarding feedback scoring.

US Example :

9823 = 9,823
10198 = 10,1K

Spain Example :

9823 = 9.823
10198 = 10.1K

Thanks

user2779312
  • 661
  • 2
  • 9
  • 23

2 Answers2

1

Try with a ..

value.ToString("#,.0K", System.Globalization.CultureInfo.CurrentUICulture);

Check out the .NetFiddle:

Output: 15.1K.

UPDATE: I created another .NetFiddle with CurrentCulture (as suggested by @Lukazoid ). Please try it from Spain and let us know if it works.

The code would be

value.ToString("#,.0K", System.Globalization.CultureInfo.CurrentCulture);
Shiva
  • 20,575
  • 14
  • 82
  • 112
  • Doing that I don't get it respecting the UI culture, I get : 15,1K , however the expected result should be : 15.1K – user2779312 Feb 13 '14 at 23:08
  • 1
    This is the correct answer and returns exactly `15.1K`. – Evan L Feb 13 '14 at 23:09
  • It returs 15.1K because you are in US. I want it to be dependent on UI Culture. – user2779312 Feb 13 '14 at 23:12
  • Added a .netFiddle example: What UI Culture are you in @user2779312 ? – Shiva Feb 13 '14 at 23:14
  • I am on Spain, but it should be culture dependent, not hardcoded or US only. – user2779312 Feb 13 '14 at 23:20
  • 1
    So, if you write it with a `.`, it becomes a `,` in a culture where this is the expected value, clearly changing depending on culture, and you're saying it's not culture dependent. I think you want it to be culture independent? – Magus Feb 13 '14 at 23:47
  • I think `CultureInfo.CurrentCulture` is what should be used when formatting values. `CultureInfo.CurrentUICulture` is for language resources. See [here](http://stackoverflow.com/questions/329033/what-is-the-difference-between-currentculture-and-currentuiculture-properties-of) – Lukazoid Feb 13 '14 at 23:55
  • Thanks @Lukazoid I created another .NetFiddle, but someone from Outside US should test it. I guess I can change the Culture on my PC too... – Shiva Feb 13 '14 at 23:59
0

So I made some code, and it works as I expect. If anyone have any ideas how to improve it, it's a pleasure to change it.

public static string GetAbbreviatedScore(int value)
{
    string result = "0";

    if (value > 0 && value < 10000)
    {
        result = value.ToString("#,#", System.Globalization.CultureInfo.CurrentUICulture);
    }
    else if (value >= 10000 && value < 100000)
    {
        if (value.ToString().EndsWith("000"))
        {
            result = value.ToString("#,#K", System.Globalization.CultureInfo.CurrentUICulture).Remove(1,4);
        }
        else
        {
        result = value.ToString("#,#K", System.Globalization.CultureInfo.CurrentUICulture).Remove(3,2);
        }
    }
    else if (value > 100000 && value < 1000000)
    {
        result = value.ToString("#,#K", System.Globalization.CultureInfo.CurrentUICulture).Remove(2,4);
    }

    return result;
}

Thank you very much

user2779312
  • 661
  • 2
  • 9
  • 23