X++ 换算运行时函数

注释

社区兴趣团体现已从 Yammer 迁移到Microsoft Viva Engage。 若要加入 Viva Engage 社区并参与最新讨论,请填写 “请求访问财务和运营 Viva Engage 社区 ”表单,然后选择要加入的社区。

本文介绍转换运行时函数。

any2Date

任意类型 值转换为 日期 值。

date any2Date(anytype object)

参数

参数 Description
对象 要转换为日期的值。

返回值

日期值。

注解

对象参数可以是大多数数据类型,但当它为 strint 类型时,会获取有用的输出。 不适当的内容生成运行时错误。

Example

static void any2DateExample(Args _args)
{
    date myDate;
    str s;
    int i;
    s = "2010 6 17"; // A string object, of yyyy mm dd.
    myDate = any2Date(s);
    Global::info(strFmt("%1  is output, from input of "2010 6 17"", myDate));
    i = 40361; // An int object, which represents the number of days from 1900/01/01.
    myDate = any2Date(i);
    Global::info(strFmt("%1  is output, from input of 40361", myDate));
}
/**** Infolog display.
Message (04:44:15 pm)
6/17/2010 is output, from input of "2010 6 17"
7/4/2010 is output, from input of 40361
****/

any2Enum

任意类型 值转换为目标枚举中元素的 Name 属性值。

enum any2Enum(anytype object)

参数

参数 Description
对象 要匹配目标枚举中元素的 Value 属性的值。

返回值

目标枚举中哪个元素的 Name 属性的值具有与输入参数匹配的 Value 属性。

注解

对象参数可以是大多数数据类型,但仅当使用 strint 类型的参数时才获取有用的数据。 此输入 对象 参数引用目标枚举中单个元素的 Value 属性。

Example

static void any2EnumExample(Args _args)
{
    NoYes myNoYes;  // NoYes is an enum.
    int i;
    str s;
    i = 0;  // An int that will be converted.
    myNoYes = any2Enum(i);
    Global::info(strfmt("'%1' - is the output, from input of the %2 as int.", myNoYes, i));
    s = "1";  // A str that will be converted.
    myNoYes = any2Enum(s);
    Global::info(strfmt("'%1' - is the output, from input of the %2 as str.", myNoYes, s));
    /**** Infolog display.
    Message (01:05:32 pm)
    'No' - is the output, from input of the 0 as int.
    'Yes' - is the output, from input of the 1 as str.
    ****/
}

any2Guid

将指定的 anytype 对象转换为 GUID 对象。

guid any2Guid(anytype object)

参数

参数 Description
对象 要转换为 GUID 对象的值。

返回值

GUID 对象。

any2Int

anytype 值转换为 int 值。

int any2Int(anytype object)

参数

参数 Description
对象 要转换的值。

返回值

int 值。

注解

对象参数可以是大多数数据类型,但仅当使用枚举实际str 类型的参数时,才会获取有用的数据。

Example

static void any2IntExample(Args _args)
{
    int myInt;
    str s;
    NoYes a;
    real r;
    s = "31";
    myInt = any2Int(s);
    Global::info(strfmt("%1 is the output, from input of 31 as a str value.", myInt));
    a = NoYes::No;
    myInt = any2Int(a);
    Global::info(strfmt("%1 is the output, from input of NoYes::No as an enum value.", myInt));
    r = 5.34e2;
    myInt = any2Int(r);
    Global::info(strfmt("%1 is the output, from the input of 5.34e2 as a real value.", myInt));
}
/**** Infolog display.
Message (02:23:59 pm)
31 is the output, from input of 31 as a str value.
0 is the output, from input of NoYes::No as an enum value.
534 is the output, from the input of 5.34e2 as a real value.
****/

any2Int64

任意类型 对象转换为 int64 对象。

int64 any2Int64(anytype object)

参数

参数 Description
对象 要转换的 anytype 对象。

返回值

int64 对象。

any2Real

任意类型 值转换为 实际 值。

real any2Real(anytype object)

参数

参数 Description
对象 要转换的值。

返回值

实际值。

注解

对象参数可以是大多数数据类型,但为日期int枚举str 类型的输入元素获取有用的输出。

Example

static void any2RealExample(Args _args)
{
    real myReal;
    str s;
    int i;
    NoYes a;
    s = "5.12";
    myReal = any2Real(s);
    Global::info(strfmt("%1 is the output from the input of 5.12 as a str object", myReal));
    i = 64;
    myReal = any2Real(i);
    Global::info(strfmt("%1 is the output from the input of 64 as an int object", myReal));
    a = NoYes::Yes;
    myReal = any2Real(a);
    Global::info(strfmt("%1 is the output from the input of NoYes::Yes as an enum object", myReal));
}
/****Infolog display.
Message (02:43:57 pm)
5.12 is the output from the input of 5.12 as a str object
64.00 is the output from the input of 64 as an int object
1.00 is the output from the input of NoYes::Yes as an enum object
****/

any2Str

任意类型 值转换为 str 值。

str any2Str(anytype object)

参数

参数 Description
对象 要转换的值。

返回值

str 值。

注解

对象参数可以是大多数数据类型,但有用的输出是从日期int枚举类型的输入元素获取的。

Example

static void any2StrExample(Args _args)
{
    str myStr;
    anytype a;
    a = "Any to string";
    myStr = any2Str(a);
    Global::info(strFmt("%1 is output, from input of Any to string as a str value", myStr));
    a = NoYes::Yes;
    myStr = any2Str(a);
    Global::info(strFmt("%1 is output, from input of NoYes::Yes as an enumeration", myStr));
}
/****Infolog Display
Message (09:08:46 am)
Any to string is output, from input of Any to string as a str value
1 is output, from input of NoYes::Yes as an enumeration
****/

anytodate

请参阅 any2Date

anytoenum

请参阅 any2Enum

anytoguid

请参阅 any2Guid

anytoint

请参阅 any2Int

anytoint64

请参阅 any2Int64

anytoreal

请参阅 any2Real

anytostr

请参阅 any2Str

char2Num

将字符串中的字符转换为字符的 ASCII 值。

int char2Num(str text, int position)

参数

参数 Description
文本消息 包含字符的字符串。
position 字符串中字符的位置。

返回值

字符的 ASCII 值作为 int 对象。

注解

char2Num("ABCDEFG",3); //Returns the numeric value of C, which is 67.
char2Num("ABCDEFG",1); //Returns the numeric value of A, which is 65.

date2Num

将日期转换为对应于自 1900 年 1 月 1 日以来的天数的整数。

int date2Num(date _date)

参数

参数 Description
_日期 要转换的日期。

返回值

1900 年 1 月 1 日与指定日期之间的天数。

Example

//Returns the value377.
date2Num(1311901);
static void date2NumExample(Args _arg)
{
    date d = today();
    int i;
    i = date2Num(d);
    print i;
}

date2Str

将指定的日期转换为字符串。

str date2Str(date date, int sequence, int day, int separator1, int month, int separator2, int year [, int flags = DateFlags::None])

参数

参数 Description
date 要转换的日期。
序列 一个三位数的数字,指示日期部分的序列: 1 表示日期的序列, 2 表示月份, 3 表示年份。
一个枚举值,该值指示日期的日期部分的格式。
separator1 一个枚举值,指示要在日期的前两个组件之间使用的分隔符。
月份 一个枚举值,该值指示日期的月份组件的格式。
separator2 一个枚举值,指示要在日期的最后两个组件之间使用的分隔符。
一个枚举值,该值指示日期的年份部分的格式。
标志 DateFlags 枚举值,该值指示是否应使用本地计算机上的语言设置来计算返回的字符串中正确的从左到右或从右到左的顺序。

返回值

一个表示指定日期的字符串。

注解

如果指定的值无效,MorphX 会将有效值分配给格式参数。 若要使用用户在区域设置中指定的日期格式,请使用 strFmtdate2Str 函数,并在所有格式参数中指定 -1 。 当区域设置控制日期格式时,设置可以从用户更改为用户。 如果 -1 用于任一分隔符参数,则两个 分隔符 默认为“区域设置”。 序列参数值必须是包含每个数字 1、2 和 3 的一个匹配项的任何三位数。 数字 1、2 和 3 分别表示日、月和年。 例如, 321 生成序列年、月和日。 或者,该值可以是 -1 以使用区域设置。 不应对此参数使用枚举类型,因为数字(如 321)超过了枚举值的有效值范围(包括 0 到 250)。 标志参数的默认值是 DateFlags::None 枚举值,这意味着不会执行从左到右或从右到左的顺序处理。

Example

以下示例按年份、月和日的顺序显示当前日期。

static void Job2(Args _args)
{
    date currentDate = today();
    str s;
    int iEnum;
    s = date2Str
    (currentDate, 
        321,
        DateDay::Digits2,
        DateSeparator::Hyphen, // separator1
        DateMonth::Digits2,
        DateSeparator::Hyphen, // separator2
        DateYear::Digits4
    );
    info("Today is:  " + s);
}
/** Example Infolog output
Message (12:36:21 pm)
Today is:  2009-01-13
**/

datetime2Str

utcdatetime 值转换为字符串。

str datetime2Str(utcdatetime datetime [, int flags = DateFlags::None])

参数

参数 Description
日期/时间 要转换的 utcdatetime 值。
标志 DateFlags 枚举值,该值指示是否对从右到左输出使用本地设置。

返回值

一个字符串,表示指定为 datetime 参数的 utcdatetime 值。

注解

Null 日期时间输入

如果为 datetime 参数指定了最小 utcdatetime 值,则 datetime2Str 函数将其视为 null 输入值。 这会导致函数返回空字符串。 DateTimeUtil::minValue 方法返回 date-time 1900-01-01T00:00:00。 此最小值被视为 null。

从右到左的本地设置

此函数的默认行为是按从左到右的顺序生成字符串,其中年份部分最左侧。 但是,如果本地设置配置为从右到左,则 DateFlags::FormatAll 枚举值的标志参数值指示函数在从右到左的顺序生成字符串。 DateTimeUtil 类的 toStr 方法的格式不受区域设置影响。

Example

static void jobTestDatetime2str( Args _args )
{
    utcdatetime utc2 = 1959-06-17T15:44:33;
    str s3;
    s3 = datetime2Str( utc2 );
    info( s3 );
}

enum2Str

将指定的枚举文本转换为字符表示形式。

str enum2Str(enum enum)

参数

参数 Description
枚举 要转换的枚举文本。

返回值

枚举的值作为字符串。

Example

以下示例返回字符串“不包括”。这是 ListCode 枚举类型的 IncludeNot 值的标签。

static void enum2StrExample(Args _arg)
{
    ListCode l;
    l =  ListCode::IncludeNot;
    print enum2Str(l);
}

guid2Str

将指定的 GUID 对象转换为等效字符串。

str guid2String(guid _uuid)

参数

参数 Description
_uuid 要转换的 GUID 对象。

返回值

与指定的 GUID 对象等效的字符串。

Example

static void guid2StrExample()
{
    guid _guid;
    str stringGuid;
    _guid = Global::guidFromString("{12345678-1234-1234-1234-123456789abc}");
    print strfmt("GUID is %1", _guid);
    stringGuid = guid2str(_guid);
    info("String GUID is " + stringGuid);
}
/**** Output to Infolog
String GUID is {12345678-1234-1234-1234-123456789ABC}
****/

int2Str

将整数转换为等效字符串。

str int2Str(int integer)

参数

参数 Description
整数 要转换的整数。

返回值

整数的字符串表示形式。

Example

static void int2StrExample(Args _arg)
{
    print "This is int2Str, value is " + int2Str(intMax());
    print "This is int642Str, value is " + int642Str(int64Max());
}

int642Str

将指定的 整数 参数转换为等效的文本字符串。

str int642Str(int64 integer)

参数

参数 Description
整数 要转换为字符串的 int64。

返回值

整数参数的等效文本字符串。

Example

static void example()
{
    print "This is int2Str, value is " + int2Str(intMax());
    print "This is int642Str, value is " + int642Str(int64Max());
}

num2Char

将整数转换为相应的 ASCII 字符。

str num2Char(int figure)

参数

参数 Description
数字 要转换为字符的整数。

返回值

由指定整数表示的字符。

Example

static void num2CharExample(Args _arg)
{
    str s;
    s = num2Char(42);
    // Prints an asterisk * -the character represented by 42.
    print s;
}

num2Date

检索与 1900 年 1 月 1 日之后指定天数相对应的日期。

date num2Date(int _days)

参数

参数 Description
_日 1900 年 1 月 1 日之后返回日期的天数。 注意: 第一个有效日期是 1901 年 1 月 1 日。 因此,除非_days超过 365否则num2Date 函数不会返回有效日期。

返回值

1900 年 1 月 1 日之后 由 _days 参数指定的日期。

注解

num2Date(366); //Returns the date 01/01/1901 (1 January 1901).

num2Str

将实数转换为字符串。

str num2Str(real number, int character, int decimals, int separator1, int separator2)

参数

参数 Description
数字 要转换为字符串的实数。
字符 文本中所需的最小字符数。
小数 所需的小数位数。
separator1 DecimalSeparator 枚举值。
separator2 ThousandSeparator 枚举值。

返回值

一个表示数字的字符串。

注解

对于 十进制 参数,最大值为 16。 如果使用了较大的数字,此方法将改为从本地计算机获取 十进制 参数的值。 在这两种情况下,都会出现舍入。 下面是 分隔符 1 参数的可能枚举值:

  • 99 – 自动(用户的格式设置确定使用的小数分隔符),枚举值 DecimalSeparator::Auto
  • 1 – Dot (.),枚举值 DecimalSeparator::D ot
  • 2 – 逗号 (,),枚举值 DecimalSeparator::Comma

下面是 separator2 参数的可能值:

  • 99 - 自动 (用户的格式设置确定使用什么千位分隔符)
  • 0 – 无(无千位分隔符),枚举值 ThousandSeparator::None
  • 1 – 点(.),枚举值 ThousandSeparator::D ot
  • 2 – 逗号 (,),枚举值 ThousandSeparator::逗号
  • 3 – 撇号 ('), 枚举值 ThousandSeparator::Apostrophe
  • 4 – Space (),枚举值 ThousandSeparator::Space

Example

在下面的代码示例中,对 num2str 方法的第一次调用为十进制参数提供 16,第二个调用提供 17

static void Job_Num2Str(Args _args)
{
    real realNum = 0.1294567890123456777; // 19 decimals places.
    info(Num2Str(realNum, 0, 16, DecimalSeparator::Dot, ThousandSeparator::Space)); // 16 decimal places
    info(Num2Str(realNum, 0, 17, DecimalSeparator::Dot, ThousandSeparator::Space)); // 17 decimal places
}

输出

这些消息位于以下 Infolog 输出中。 输出中的第一个数字包含 16 位小数位数,而第二个数字仅包含两个小数位数。

Message (10:18:12)
0.1294567890123457
0.13

str2Date

将指定的字符串转换为 日期 值。

date str2Date(str _text, str _sequence)

参数

参数 Description
_发短信 要转换为 日期 值的字符串。
_序列 一个三位数整数,描述要转换的字符串中日、月和年的位置。

返回值

日期值。

注解

使用以下值指定 _sequence 参数中的日、月和年的位置:

  • 日期: 1
  • 月份: 2
  • 年份: 3

例如,如果字符串中的序列为 month、year 和 day, 则 _sequence 参数必须为 231。 如果输入参数指定无效日期,则返回 0 (零) 日期。 以下两个示例指定无效日期。

str2Date("31/12/44", 123) // Year must be four digits to reach the minimum of January 1 1901.
str2Date("31/12/2044", 213) // 213 means the month occurs first in the string, but 31 cannot be a month.

Example

static void str2DateExample(Args _arg)
{
    date d;
    d = str2Date("22/11/2007", 123);
    print d;
}

str2Datetime

从指定的日期和时间信息字符串中生成 utcdatetime 值。

utcdatetime str2datetime( str text, int sequence )

参数

参数 Description
文本消息 要转换为 utcdatetime 值的字符串。
序列 描述 文本 参数中日期组件的序列的三位数。

返回值

表示指定日期和时间的 utcdatetime 值。

注解

文本参数的日期部分的语法要求很灵活。 各种有效格式与 date2str 函数中的格式相同。 对 str2datetime 的以下每个调用都有效,所有这些调用都会生成相同的输出。

utc3 = str2datetime( "1985/02/25 23:04:59" ,321 );
utc3 = str2datetime( "Feb-1985-25 11:04:59 pm" ,231 );
utc3 = str2datetime( "2 25 1985 11:04:59 pm" ,123 );

日期时间的每个组件都由 序列 参数中的数字表示:

  • 1 - 天
  • 2 – 月
  • 3 – 年份

例如,年、月、日顺序为 321。 所有有效值均包含这三个数字中的每一个正好一次。 如果 序列 参数的值无效,则区域设置用于解释输入 文本 参数。 如果输入参数描述无效的日期和时间,则返回空字符串。

Example

static void JobTestStr2datetime( Args _args )
{
    utcdatetime utc3;
    str sTemp;
    utc3 = str2datetime( "1985/02/25 23:04:59" ,321 );
    sTemp = datetime2str( utc3 );
    print( "sTemp == " + sTemp );
}

str2Enum

检索本地化 的 Label 属性值与输入字符串匹配的枚举元素。

enum str2Enum(enum _type, str _text)

参数

参数 Description
_type 声明 枚举 类型的变量。
_发短信 枚举中目标元素的本地化 Label 属性文本。

返回值

目标枚举的元素,也表示 int。

注解

相关函数 枚举 2str 从枚举中的一个元素返回 Label 属性的值。 enum2str 函数返回的值可以是 str2enum 函数的 _type 参数的输入。 _text参数的相应值为 enum2Str(BankAccountType::SavingsAccount)。 枚举的每个元素都有 一个 Name 属性和 一个 Label 属性。 在全新安装中, Name 值几乎始终是英语单词。 在英语版本中, Label 属性值几乎始终与 Name 值相同。 但是,在非英语版本中, 标签 值已本地化,因此与 Name 值不匹配。

Example

若要避免由本地化为其他口语引起的字符串不匹配,建议使用 enum2str 函数生成 str2enum 函数的输入。 以下示例演示了将 str2enum 函数与 枚举 2str 函数一起使用的适当方法。

static void str2Enum_AcrossLangs(Args _arg)
{
    BankAccountType bat;
    str sEnumValueLabelLocalized;
    int nInt;
    // enum2str.
    sEnumValueLabelLocalized = enum2str(BankAccountType::SavingsAccount);
    info("Localized friendly string: "
        + sEnumValueLabelLocalized);
    // str2enum.
    bat = str2Enum(bat, sEnumValueLabelLocalized);
    nInt = bat;
    info("nInt = " + int2str(nInt));
    /********** Actual output:
    Message (04:32:12 pm)
    Localized friendly string: Savings account
    nInt = 1
    **********/
}

str2Guid

将字符串转换为 GUID 对象。

Guid str2Guid(str text)

参数

参数 Description
guid 表示 GUID 的字符串。

返回值

由输入字符串表示的 GUID。

注解

例如, guid 参数的有效值为 {12345678-1234-abCD-3456-123456789012}(带或不使用大括号)。

str2Int

将字符串转换为等效整数。

int str2Int(str _text)

参数

参数 Description
_发短信 要转换为整数的字符串。

返回值

指定字符串的等效整数。

Example

static void str2IntExample(Args _arg)
{
    int i;
    i = str2Int("1234567890");
    print "i = " + int2Str(i);
}

str2Int64

将字符串转换为 Int64 值。

int str2Int64(str text)

参数

参数 Description
文本消息 要转换的字符串。

返回值

指定字符串的 Int64 值。

Example

static void str2Int64Example(Args _args)
{
    str myStr;
    str tooBig;
    Int64 myInt64;
    myStr = "1234567890";
    tooBig = int642str(int64Max()+1);
    myInt64 = str2Int64(mystr);
    print strfmt ("int64: %1",myInt64);
    myInt64 = str2Int64(tooBig);
    print strfmt ("Too big int64: %1",myInt64);
}

str2Num

将字符串转换为实数。

real str2Num(str _text)

参数

参数 Description
_发短信 要转换为实数的字符串。

返回值

如果指定的字符串包含有效数字,则为实数;否则为 0 (零)。

注解

以下示例演示了如何使用此函数。

str2Num("123.45") returns the value 123.45.
str2Num("a123") returns the value 0.0.
str2Num("123a") returns the value 123.00.

当无法将字符转换为实数的一部分时,扫描从左到右开始。

Example

static void str2NumToReal(Args _arg)
{
    real r;
    str s;
    r = str2Num("3.15");
    s = strFmt("r = %1", r);
    info(s);
}
/*** Infolog output.
Message_@SYS14327 (02:36:12 pm)
r = 3.15
***/

static void str2NumExponentialSyntax(Args _args)
{
    Qty qty1, qty2, qty3;
    qty1 = str2num('1e-3'); // Bad syntax by the user.
    qty2 = str2num('1.e-3');
    qty3 = str2num('1.0e-3');
    info(strfmt('Result: %1; Expected: %2', num2str(qty1, 0,3,2,0), '0.001'));
    info(strfmt('Result: %1; Expected: %2', num2str(qty2, 0,3,2,0), '0.001'));
    info(strfmt('Result: %1; Expected: %2', num2str(qty3, 0,3,2,0), '0.001'));
}
/*** Infolog output. The first result differs from expectations.
Message_@SYS14327 (02:20:55 pm)
Result: 1,000; Expected: 0.001
Result: 0,001; Expected: 0.001
Result: 0,001; Expected: 0.001
***/

str2Time

将字符串转换为 timeOfDay 值。

int str2Time(str _text)

参数

参数 Description
_发短信 用于计算自午夜以来的秒数的时间。

返回值

午夜和 _text 参数之间的秒数;否则为 -1

注解

str2Time("05:01:37") //Returns the value 18097.
str2Time("7 o'clock") //Returns the value -1.

Example

static void str2TimeExample(Args _arg)
{
    int i;
    i = str2Time("11:30");
    print i;
}

time2Str

timeOfDay 值转换为包含小时、分钟和秒的字符串。

str time2Str(int _time, int _separator, int _timeFormat)

参数

参数 Description
_时间 timeOfDay 值。
_分隔符 TimeSeparator 枚举值,该值指示输出字符串中小时、分钟和秒之间的字符。
_timeFormat 一个 TimeFormat 枚举值,该值指示是使用 12 小时制还是使用 24 小时制。

返回值

一个表示指定时间的字符串。

注解

_time 参数的值是自午夜以来的秒数。

Example

static void TimeJob4(Args _args)
{
    timeOfDay theTime = timeNow();
    info( time2Str(theTime, TimeSeparator::Colon, TimeFormat::AMPM) );
}
/**
Message (04:33:56 pm)
04:33:56 pm
**/

uint2Str

将整数转换为字符串。 假设整数为无符号。

str uint2Str(int integer)

参数

参数 Description
整数 要转换的整数。

返回值

等效于指定的无符号整数的字符串。

注解

将此函数而不是 int2str 函数用于非常大的整数,例如记录 ID。

info(int2str(3123456789)); //returns -1171510507 as a string.
info(uint2str(3123456789)); //returns 3123456789 as a string.