0

So I am working on a project for myself.

Now I am using string.formatter

bon = bon + String.Format("{0,-2} X {1,-10}  {1,5}", hoeveelheid[i], frieten[i], hulp);

The problem lays in "hulp" this is a double. But it is not showed in the output. instead the same output as frieten[i] is outputted.

Like so :

4 X klein klein

The output needs to be like : `

4X klein €15,00

`

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
user3574537
  • 1
  • 1
  • 4

2 Answers2

1

You just need to change your {1,5} to {2,5}.

Also if it is a double value, you might need to format it with The "C" format specifier.

You can create own custom NumberFormatInfo and format your double as;

var nfi = new NumberFormatInfo()
{
     CurrencySymbol = "€",
     CurrencyDecimalSeparator = ",",
     CurrencyPositivePattern = 0,
     CurrencyDecimalDigits = 2
};

and

bon = bon + String.Format("{0,-2} X {1,-10}  {2,5}", 
                          hoeveelheid[i], frieten[i], hulp.ToString("C", nfi));

By the way: What is the best data type to use for money in c#?

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

You are repeating your second variable twice:

Change the last format from : {0,-2} X {1,-10} {1,5} to {0,-2} X {1,-10} {2,5}

kemiller2002
  • 113,795
  • 27
  • 197
  • 251