1

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

2000

into a format like this:

2.000,00

I have tried the following methods:

String.valueOf(input.format());

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 ?

User987
  • 3,663
  • 15
  • 54
  • 115

2 Answers2

2

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;
    Console.WriteLine(i.ToString("#,##0.00"));
    Console.ReadLine();

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

K. Berger
  • 361
  • 1
  • 9
  • would this imply for bigger numbers than 2000 ? like 20000 ? 20.000,00 ? – User987 Dec 14 '16 at 15:06
  • Yes, as said, read about providers on msdn. My example makes sure that digit froups are delimited by a comma and that there is a 0, when passing values < 1 – K. Berger Dec 14 '16 at 15:08
1

given en-US culture this will do:

string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:N}", 2000)
Alexander Taran
  • 6,655
  • 2
  • 39
  • 60