I recently encountered a situation in AX where I needed to convert a number into a string that was specific to the current user’s language setting.
AX provides a method called num2str for converting a real number into a formatted string. However, this function requires the user to specify in code which separators are to be used in the output and is not specific to the language of the current user. In order properly format the output based on the user’s language preference, you would need to parse the culture information for the language and then pass those values in to the AX method. You would also need to add any currency identifier if the output was to be used as a currency.
To resolve this problem I created a static method on the Global class that utilizes the culture specific number formatting available in the .Net framework. This method allows you to specify the output format as well as the desired precision and is outlined below.
public static str num2LangStr(
real _value,
LanguageId _languageId = infolog.language(),
slcNumberFormat _format = slcNumberFormat::FixedPoint,
boolean _useFormatPrecision = true,
int _precision = 3)
{
System.Globalization.CultureInfo ci;
System.Decimal dec;
str result;
str format = enum2str(_format);
System.Exception clrException;
try
{
ci = System.Globalization.CultureInfo::CreateSpecificCulture(_languageId);
dec = _value;
if(!_useFormatPrecision)
{
format += int2str(_precision);
}
result = dec.ToString(format,ci);
}
catch(Exception::CLRError)
{
clrException = CLRInterop::getLastException();
throw error(clrException.ToString());
}
return result;
}
The slcNumberFormat
is an enumeration that contains the available output formats and is included in the attached XPO.
The XPO also includes a job that demonstrates usage of the method.
Enjoy!
The following file contains a project with the classes used in this post. SLC_LanguageFormattedNumber