共用方式為


X++ 轉換執行階段函式

備註

社區興趣小組現在已從 Yammer 轉移到 Microsoft Viva Engage。 若要加入 Viva Engage 社群並參與最新的討論,請填寫 [ 要求存取財務和營運 Viva Engage 社群 表單 」 ,並選擇您要加入的社群。

本文說明轉換執行階段函式。

any2日期

anytype 值轉換為 日期 值。

date any2Date(anytype object)

參數

參數 Description
物件 要轉換為日期的值。

返回值

日期值。

備註

object 參數可以是大多數數據類型,但當它是 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
****/

any2列舉

anytype 值轉換為目標列舉中元素的 Name 屬性值。

enum any2Enum(anytype object)

參數

參數 Description
物件 符合目標列舉中元素的 Value 屬性的值。

返回值

目標列舉中任何元素的 Name 屬性值具有符合輸入參數的 Value 屬性。

備註

object 參數可以是大部分的資料類型,但只有在使用 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 物件。

任何2Int

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.
****/

任何2Int64

anytype 物件轉換為 int64 物件。

int64 any2Int64(anytype object)

參數

參數 Description
物件 要轉換的 anytype 物件。

返回值

int64 物件。

任何2真實的

anytype 值轉換為 實際 值。

real any2Real(anytype object)

參數

參數 Description
物件 要轉換的值。

返回值

真正的價值。

備註

object 參數可以是大多數資料類型,但會針對 dateintenumstr 類型的輸入元素取得有用的輸出。

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
****/

任何2Str

anytype 值轉換為 str 值。

str any2Str(anytype object)

參數

參數 Description
物件 要轉換的值。

返回值

str 值。

備註

object 參數可以是大多數資料類型,但有用的輸出是從 dateintenum 類型的輸入元素取得的。

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
****/

任何日期

請參閱 any2Date

anytoenum

請參閱 any2Enum

任何圖吉德

請參閱 any2Guid

任意英特

請參閱 any2Int

任意英特64

請參閱 any2Int64

任意實數

參見 any2Real

任何托斯特

參見 any2Str

char2Num

將字串中的字元轉換為字元的 ASCII 值。

int char2Num(str text, int position)

參數

參數 Description
收發簡訊 包含字元的字串。
位置 字元在字串中的位置。

返回值

字元的 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.

日期2編號

將日期轉換為對應於自 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;
}

日期2Str

將指定的日期轉換為字串。

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 代表年。
列舉值,指出日期的日期元件格式。
分隔符號1 列舉值,指出要在日期前兩個元件之間使用的分隔符號。
列舉值,指出日期的月份元件格式。
分隔符號2 列舉值,指出要在日期最後兩個元件之間使用的分隔符號。
列舉值,指出日期年份元件的格式。
flags DateFlags 列舉值,指出是否應該使用本機電腦上的語言設定來計算傳回字串中正確的從左到右或從右到左的順序。

返回值

代表指定日期的字串。

備註

如果指定的值無效,MorphX 會將有效值配置給格式參數。 若要使用使用者在「區域設定」中指定的日期格式,請使用 strFmtdate2Str 函數,並在所有格式參數中指定 -1 。 當區域設定控制日期格式時,設定可能會因使用者而異。 如果 -1 用於任一分 隔符號 參數,則這兩個分隔符號都會預設為區域設定。 序列參數值必須是任何三位數的數字,其中每個數字 1、2 和 3 都只包含一次出現。 數字 1、2 和 3 分別代表日、月和年。 例如, 321 會產生序列年、月和日。 或者,值可以是 -1 以使用區域設定。 此參數不應使用列舉類型,因為 321 等數字超出列舉值的有效值範圍,即 0 到 250 (含)。 flags 參數的預設值是 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
**/

日期時間2Str

utcdatetime 值轉換為字串。

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

參數

參數 Description
日期時間 要轉換的 utcdatetime 值。
flags DateFlags 列舉值,指出是否要使用從右至左輸出的本機設定。

返回值

代表指定為 datetime 參數之 utcdatetime 值的字串。

備註

Null 日期時間輸入

如果為 datetime 參數指定最小 utcdatetime 值,則 datetime2Str 函數會將它視為空值輸入值。 這會導致函數傳回空字串。 DateTimeUtil::minValue 方法會傳回日期時間 1900-01-01T00:00:00。 此最小值會被視為空值。

從右到左的本機設定

此函式的預設行為是以從左到右的順序產生字串,其中年份部分位於最左邊。 不過,如果本機設定為從右至左,則 DateFlags::FormatAll 列舉值的 flags 參數值會指示函式以從右至左的順序產生字串。 DateTimeUtil 類別的 toStr 方法格式不受區域設定的影響。

Example

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

列舉2Str

將指定的列舉文字轉換為字元表示法。

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;
}

num2日期

擷取對應於 1900 年 1 月 1 日之後指定天數的日期。

date num2Date(int _days)

參數

參數 Description
_日 1900 年 1 月 1 日之後要傳回日期的天數。 便條: 第一個有效日期是 1901 年 1 月 1 日。 因此, num2Date 函數不會傳回有效日期,除非 _days 超過 365

返回值

日期,即 _days 參數在 1900 年 1 月 1 日之後指定的天數。

備註

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
數字 要轉換為字串的實數。
字元 文字中所需的字元數下限。
小數 所需的小數位數。
分隔符號1 DecimalSeparator 列舉值。
分隔符號2 ThousandSeparator 列舉值。

返回值

代表數字的字串。

備註

對於 小數 參數,最大值為 16。 如果使用較大的數字,這個方法會改為從本機電腦取得 小數 參數的值。 在這兩種情況下,都會發生四捨五入。 以下是 separator1 參數的可能列舉值:

  • 99 – 自動 (使用者的格式設定會決定使用的小數點分隔符號),列舉值 DecimalSeparator::Auto
  • 1 – 點 (.),列舉值 DecimalSeparator::D ot
  • 2 – 逗號 (,),列舉值 DecimalSeparator::Comma

以下是 separator2 參數的可能值:

  • 99 – 自動(用戶的格式設置決定使用哪千位分隔符)
  • 0 – 無 (沒有千位分隔符號),列舉值 ThousandSeparator::None
  • 1 – 點 (.),列舉值 ThousandSeparator::D ot
  • 2 – 逗號 (,),列舉值 ThousandSeparator::Comma
  • 3 – 撇號 ('),列舉值 ThousandSeparator::A撇號
  • 4 – 空格 ( ),列舉值 ThousandSeparator::Space

Example

在下列程式碼範例中,第一次呼叫 num2str 方法會提供 16 作為 decimals 參數,而第二個呼叫會提供 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

str2日期

將指定的字串轉換為 日期 值。

date str2Date(str _text, str _sequence)

參數

參數 Description
_簡訊 要轉換為 日期 值的字串。
_序 一個三位數的整數,描述要轉換的字串中日、月和年的位置。

返回值

日期值。

備註

使用下列值來指定 _sequence 參數中日、月和年的位置:

  • 天數: 1
  • 月: 2
  • 年份: 3

例如,如果字串中的順序是月、年,然後是日,則 _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;
}

str2日期時間

從指定的日期和時間資訊字串產生 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 );
}

str2列舉

擷取當地語系化 Label 屬性值符合輸入字串的列舉元素。

enum str2Enum(enum _type, str _text)

參數

參數 Description
_type 宣告為 列舉 類型的變數。
_簡訊 列舉中目標元素的當地語系化 Label 屬性文字。

返回值

目標列舉的元素,也代表 int。

備註

相關函式 enum2str 會從列舉中的一個元素傳回 Label 屬性的值。 enum2str 函數傳回的值可以是 str2enum 函數的 _type 參數的輸入。 _text 參數的適當值是 enum2Str(BankAccountType::SavingsAccount)。 列舉的每個元素都有 Name 屬性和 Label 屬性。 在全新安裝中, 名稱 值幾乎一律是英文單字。 在英文版中, Label 屬性值幾乎一律與 Name 值相同。 不過,在非英文版本中, 標籤 值已當地語系化,因此不符合 名稱 值。

Example

若要避免因當地語系化為其他口語而導致字串不相符,建議您使用 enum2str 函式來產生 str2enum 函式的輸入。 下列範例顯示將 str2enum 函式與 enum2str 函式搭配使用的適當方式。

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);
}

str2編號

將字串轉換為實數。

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
***/

str2時間

將字串轉換為 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;
}

時間2Str

timeOfDay 值轉換為包含小時、分鐘和秒的字串。

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

參數

參數 Description
_時間 timeOfDay 值。
_separator 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
整數 要轉換的整數。

返回值

相當於指定不帶正負號整數的字串。

備註

針對非常大的整數 (例如記錄 ID) 使用此函式,而不是 int2str 函式。

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