user6555550
user6555550

Reputation:

Format String to Currency in Linq

Is it possbile to convert a string via LINQ to a currency within the following code: (I would like to get the following Format :1.234,56

} into g        
   select new        
   {
       Kunde_Kz = g.Key.Kunde_Kz,
       Geschäftspartner = g.Key.Geschäftspartner,
       Art_Gruppe = g.Key.Art_Gruppe,
       //Auftragsposition = g.Key.Auftragsposition,
       Deckungsbeitrag = g.Sum(x => x.Field<double>("Deckungsbeitrag")),
       Erlös = g.Sum(x => x.Field<double>("Erlös")),
       Dienst = g.Sum(x => x.Field<double>("Dienst")),
       IS_Erlös = g.Sum(x => x.Field<double>("IS_Erlös")),
       Ware_in = g.Sum(x => x.Field<double>("Ware_in")),
       Ware_out = g.Sum(x => x.Field<double>("Ware_out")),
       Mieten = g.Sum(x => x.Field<double>("Mieten")),
       Kosten = g.Sum(x => x.Field<double>("Kosten")),
       Dienst_kosten = g.Sum(x => x.Field<double>("Dienst_kosten")),
       Verarbeitung = g.Sum(x => x.Field<double>("Verarbeitung")),
       Ware_in_kosten = g.Sum(x => x.Field<double>("Ware_in_kosten")),
       Ware_out_kosten = g.Sum(x => x.Field<double>("Ware_out_kosten")),
       Mieten_kosten = g.Sum(x => x.Field<double>("Mieten_kosten"))

   }).ToList();

Upvotes: 0

Views: 1383

Answers (2)

mortb
mortb

Reputation: 9859

You could write:

Mieten_kosten = g.Sum(x => x.Field<double>("Mieten_kosten")).ToString("c")

Then the Mieten_kosten variable will contain a string that has a currency symbol. Assuming that the machine has a German locale setting the Mieten_kosten would formatted as you write currency and be suffixed by a euro sign (€).

A detailed example:

var cult = System.Globalization.CultureInfo.GetCultureInfo("de-DE"); // make sure we get the german locale
(12222.3).ToString("c", cult).Dump(); // outputs 12.222,30 €

Upvotes: 0

Fixation
Fixation

Reputation: 989

'N' ToString format: MSDN

Summary: The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol. The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, the number of decimal places is defined by the current NumberFormatInfo.NumberDecimalDigits property.

123456d.ToString("N2") // outputs "123.456,00" or "123,456.00" depending on culture

Upvotes: 2

Related Questions