Converting int to string with a specific format


I'm trying to convert an classical integer value like:


into a format like this:


I have tried the following methods:


And this method:

private String getCents(Decimal x){
    String y = String.valueOf(x);
    String z = '.';
    if(y.contains(',')) z = ',';
    y = y.substring(0, y.indexOf(z));
    if(x - Decimal.valueOf(y) == 0)
        return String.valueOf(x.format()) + z + '00';
    else return String.valueOf(x.format());

But the string class doesn't contains the valueOf method for some reason. Is there any other way to do this ?

Show source
| c#   | integer   | string   | winforms   | value   2016-12-14 15:12 2 Answers

Answers to Converting int to string with a specific format ( 2 )

  1. 2016-12-14 16:12

    given en-US culture this will do:

    string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:N}", 2000)
  2. 2016-12-14 16:12

    The class String does not contain the method valueOf, because this ain't Java, you know. The method you are searching for is ToString, which allows a format-provider as an argument. The simplest way is another string which defines the format.

        int i = 2000;

    This will do what you want to do. Read more about format-providers in the docs of the ToStringmethod.

Leave a reply to - Converting int to string with a specific format

◀ Go back