mercoledì 22 febbraio 2017

AX 2012,Dynamics 365 - Convertire date e numeri in base alla Culture

Ciao in questo articolo posto 2 piccoli metodi che ho scritto per convertire date e numeri in base alla lingua passata come parametro:

Questo per le date:

 public static str Date2StringByLanguage(TransDate _transDate, LanguageId _languageId)
   {
     str                   timeFormat;
     System.DateTime             thisDate;
     System.Globalization.CultureInfo    cultInfo;
     System.Globalization.DateTimeFormatInfo formatInfo;
     ;
     thisDate = new System.DateTime( year(_transDate),
                   mthOfYr(_transDate),
                   dayOfMth(_transDate));
     cultInfo = System.Globalization.CultureInfo::CreateSpecificCulture(_languageId);
     formatInfo = cultInfo.get_DateTimeFormat();
     timeFormat = formatInfo.get_ShortDatePattern();
     return thisDate.ToString(timeFormat);
   }

Questo per i numeri:

 static str Num2StringByLanguage(real _num, LanguageId _languageId)
   {
     str                   numformat;
     System.Globalization.CultureInfo    cultInfo;
     System.Globalization.NumberFormatInfo  formatInfo;
     str                   NumberDecimalSeparator;
     str                   NumberGroupSeparator;
     int                   DecimalSeparator;
     int                   ThousandSeparator;
     cultInfo = System.Globalization.CultureInfo::CreateSpecificCulture(_languageId);
     formatInfo = cultInfo.get_NumberFormat();
     NumberDecimalSeparator = formatInfo.get_NumberDecimalSeparator();
     NumberGroupSeparator = formatInfo.get_NumberGroupSeparator();
     switch(NumberDecimalSeparator)
     {
       case ".":
         DecimalSeparator = 1;
         break;
       case ",":
         DecimalSeparator = 2;
         break;
       default:
         DecimalSeparator = -1;
         break;
     }
     switch(NumberGroupSeparator)
     {
       case ".":
         ThousandSeparator = 1;
         break;
       case ",":
         ThousandSeparator = 2;
         break;
       case " ":
         ThousandSeparator = 3;
         break;
       default:
         ThousandSeparator = -1;
         break;
     }
     return num2str(_num,-1,-1,DecimalSeparator,ThousandSeparator);
   }

Nessun commento:

Posta un commento