PowerApps 中的 Text 函数Text function in PowerApps

设置数字或日期/时间值的格式,使其作为文本字符串显示。Formats a number or a date/time value for display as a string of text.

说明Description

Text 函数根据下面的一种参数类型设置数字或日期/时间值的格式:The Text function formats a number or a date/time value based on one of these types of arguments:

  • 预定义的日期/时间格式,可使用 DateTimeFormat 枚举指定。A predefined date/time format, which you specify by using the DateTimeFormat enumeration. 对于日期和时间,首选此方法,因为它会根据每个用户的语言和地理位置进行自动调整。For dates and times, this approach is preferred as it automatically adjusts to each user's language and location.
  • 自定义格式,包含描述了如何设置数字或日期/时间值格式的占位符的文本字符串。A custom format, a string of text that comprises placeholders that describe how to format the number or the date/time value. 占位符定义了要显示的位数、是否应使用分组分隔符及如何显示月份名称。Placeholders define how many digits to show, whether grouping separators should be used, and how to display the name of a month. PowerApps 支持 Microsoft Excel 支持的一部分占位符。PowerApps supports a subset of the placeholders that Microsoft Excel does.

有关详细信息,请参阅处理日期和时间See working with dates and times for more information.

预定义的日期/时间格式Predefined date/time formats

预定义的格式Predefined Format 说明Description
DateTimeFormat.LongDateDateTimeFormat.LongDate 完整的年份、月份、相应月份中的多少号和星期几。Full year, month, day of the month, and day of the week. 月份名称和星期几不采用缩写形式。The names of the month and the day of the week aren't abbreviated.
DateTimeFormat.LongDateTimeDateTimeFormat.LongDateTime 完整的年份、月份、相应月份中的多少号和星期几,外加时(12 小时制)、分、秒和 AM/PM 标记。Full year, month, day of the month, and day of the week, plus hour (12-hour clock), minutes, seconds, and AM/PM designation. 月份名称和星期几不采用缩写形式。The names of the month and the day of the week aren't abbreviated.
DateTimeFormat.LongDateTime24DateTimeFormat.LongDateTime24 完整的年份、月份、相应月份中的多少号和星期几,外加时(24 小时制)、分和秒。Full year, month, day of the month, and day of the week, plus hour (24-hour clock), minutes, and seconds. 月份名称和星期几不采用缩写形式。The names of the month and the day of the week aren't abbreviated.
DateTimeFormat.LongTimeDateTimeFormat.LongTime 时(12 小时制)、分、秒和 AM/PM 标记。Hour (12-hour clock), minutes, seconds, and AM/PM designation. 与 ShortTime 相同。Same as ShortTime.
DateTimeFormat.LongTime24DateTimeFormat.LongTime24 时(24 小时制)、分和秒。Hour (24-hour clock), minutes, seconds. 与 ShortTime24 相同。Same as ShortTime24.
DateTimeFormat.ShortDateDateTimeFormat.ShortDate 四位数的年份和两位数的月份及相应月份中的多少号。Four-digit year with two-digit month and day of the month.
DateTimeFormat.ShortDateTimeDateTimeFormat.ShortDateTime 四位数的年份和两位数的月份及相应月份中的多少号,外加时(12 小时制)、分、秒和 AM/PM 标记。Four-digit year with two-digit month and day of the month, plus hour (12-hour clock), minutes, seconds, and AM/PM designation.
DateTimeFormat.ShortDateTime24DateTimeFormat.ShortDateTime24 四位数的年份和两位数的月份及相应月份中的多少号,外加时(24 小时制)、分和秒。Four-digit year with two-digit month and day of the month, plus hour (24-hour clock), minutes, and seconds.
DateTimeFormat.ShortTimeDateTimeFormat.ShortTime 时(12 小时制)、分、秒和 AM/PM 标记。Hour (12-hour clock), minutes, seconds, and AM/PM designation. 与 LongTime 相同。Same as LongTime.
DateTimeFormat.ShortTime24DateTimeFormat.ShortTime24 时(24 小时制)、分和秒。Hour (24-hour clock), minutes, and seconds. 与 LongTime24 相同。Same as LongTime24.
DateTimeFormat.UTCDateTimeFormat.UTC 根据当前用户所在的时区将日期/时间值转换为 UTC,并根据 ISO 8601 标准设置格式。The date/time value is converted to UTC based on the current user's time zone and formatted according to the ISO 8601 standard.

数字占位符Number placeholders

占位符Placeholder 说明Description
00 (zero) 如果数字的位数少于包含零的格式中的位数,显示无意义的零。Displays insignificant zeros if a number has fewer digits than there are zeros in the format. 例如,如果要将 8.9 显示为 8.90,请使用 #.00 格式。For example, use the format #.00 if you want to display 8.9 as 8.90.
# 0(零)的规则相同。Follows the same rules as the 0 (zero). 不过,如果数字的小数点前/后位数少于包含 # 符号的格式中的位数,Text 不返回多余的零。However, Text doesn't return extra zeros when the number has fewer digits on either side of the decimal than there are # symbols in the format. 例如,如果自定义格式为 #.##,而要设置格式的数字为 8.9,则显示的是 8.9For example, 8.9 is displayed if the custom format is #.## and the number to format is 8.9.
.. 句点(period) 在数字中显示小数点。Displays the decimal point in a number. 视自定义格式的语言而定。请参阅全局应用,了解更多详情。Depends on the language of the custom format, see global apps for more details.
,逗号, (comma) 在数字中显示分组分隔符,通常是千位分隔符。Displays the grouping separator in a number, often used for thousands. 如果格式中的逗号的前后是数字记号 (#) 或零,Text 使用逗号分隔各组。Text separates groups by commas if the format contains a comma that's enclosed by number signs (#) or by zeros. 视自定义格式的语言而定。请参阅全局应用,了解更多详情。Depends on the language of the custom format, see global apps for more details.

如果数字的小数点后面的位数多于包含占位符的格式中的位数,则数字四舍五入为与占位符相同的位数。If a number has more digits to the right of the decimal point than there are placeholders in the format, the number rounds to as many decimal places as there are placeholders. 如果小数点前面的位数多于包含占位符的格式中的位数,则显示多余的位数。If there are more digits to the left of the decimal point than there are placeholders, the extra digits are displayed. 如果格式中小数点前面仅包含数字记号 (#),则小于 1 的数字以小数点开头(例如,.47)。If the format contains only number signs (#) to the left of the decimal point, numbers less than 1 start with a decimal point (for example, .47).

日期和时间占位符Date and time placeholders

占位符Placeholder 说明Description
mm 将月份显示为不带前导零的数字。Displays the month as a number without a leading zero.
mmmm 视情况将月份显示为带前导零的数字。Displays the month as a number with a leading zero when appropriate.
mmmmmm 以缩写形式(JanDec)显示月份。Displays the month as an abbreviation (Jan to Dec).
mmmmmmmm 以全称形式(JanuaryDecember)显示月份。Displays the month as a full name (January to December).
dd 将日期显示为不带前导零的数字。Displays the day as a number without a leading zero.
dddd 视情况将日期显示为带前导零的数字。Displays the day as a number with a leading zero when appropriate.
dddddd 以缩写形式(SunSat)显示日期。Displays the day as an abbreviation (Sun to Sat).
dddddddd 以全称形式(SundaySaturday)显示日期。Displays the day as a full name (Sunday to Saturday).
yyyy 将年份显示为两位数的数字。Displays the year as a two-digit number.
yyyyyyyy 将年份显示为四位数的数字。Displays the year as a four-digit number.
hh 将小时显示为不带前导零的数字。Displays the hour as a number without a leading zero.
hhhh 视情况将小时显示为带前导零的数字。Displays the hour as a number with a leading zero when appropriate. 如果格式中包含 AMPM,以 12 小时制显示小时。If the format contains AM or PM, the hour is shown based on the 12-hour clock. 如果不包含,以 24 小时制显示小时。Otherwise, the hour is shown based on the 24-hour clock.
mm 将分显示为不带前导零的数字。Displays the minute as a number without a leading zero. > [!NOTE]
> m 或 mm 代码必须紧跟在 h 或 hh 代码后面,或后面紧跟 ss 代码;否则 Text 返回的是月份,而不是分钟。> The m or the mm code must appear immediately after the h or hh code or immediately before the ss code; otherwise, Text returns the month instead of minutes.
mmmm 视情况将分显示为带前导零的数字。Displays the minute as a number with a leading zero when appropriate. > [!NOTE]
> m 或 mm 占位符必须紧跟在 h 或 hh 占位符后面,或后面紧跟 ss 占位符。> The m or the mm placeholder must appear immediately after the h or hh placeholder or immediately before the ss placeholder. 否则,Text 会返回月份,而不是分。Otherwise, Text returns the month instead of minutes.
ss 将秒显示为不带前导零的数字。Displays the second as a number without a leading zero.
ssss 视情况将秒显示为带前导零的数字。Displays the second as a number with a leading zero when appropriate.
ff 显示秒的小数部分。Displays the fractions of seconds.
AM/PMam/pmA/Pa/pAM/PM, am/pm, A/P, a/p 以 12 小时制显示小时。Displays the hour based on a 12-hour clock. Text 对午夜至正午这一时间段的时间返回“AM”、“am”、“A”或“a”,对正午至午夜这一时间段的时间返回“PM”、“pm”、“P”或“p”。Text returns "AM", "am", "A", or "a" for times from midnight until noon and "PM", "pm", "P", or "p" for times from noon until midnight

文本占位符Literal placeholders

可以在格式字符串中添加以下任意字符。You can include any of these characters in your format string. 这些字符将按原样显示在 Text 结果中。They will appear in the result of Text as is. 其他字符留待用于以后的占位符,因此不得使用其他字符。Additional characters are reserved for future placeholders, so you shouldn't use them.

字符Character 说明Description
任意货币符号Any currency symbol 美元符号、分币符号、欧元符号等Dollar sign, cents sign, euro sign, etc.
+ 正号Plus sign
(( 左小括号Left parenthesis
:: 冒号Colon
^ 扬抑符(脱字号)Circumflex accent (caret)
'' 撇号Apostrophe
{{ 左花括号Left curly bracket
< 小于号Less-than sign
= 等于号Equal sign
- 负号Minus sign
/ 左斜线Slash mark
)) 右小括号Right parenthesis
& & 号Ampersand
~ 波形符Tilde
}} 右花括号Right curly bracket
> 大于号Greater-than sign
  空格字符Space character

全局应用Global apps

Text 函数具有全局感知能力。The Text function is globally aware. 对于各种语言,此函数都能确定如何正确写出日期、时间、货币和数字。For a wide array of languages, it knows how to properly write out dates, times, currencies, and numbers. 它需要获取以下两条信息才能正常运行:To do its job, it needs two pieces of information:

  • 自定义格式的语言:从作者角度出发,需要考虑的是,应如何解读自定义格式。The language of the custom format: For authors, how should a custom format be interpreted? 分隔符(. The separator characters (. ,)的含义因语言而异。and ,) have different meanings in different languages. 这需要通过包含语言标记的特殊占位符进行处理。This is handled with a special placeholder containing a language tag. 预定义的日期/时间格式未指定语言,这就更简单了。Even easier, the predefined date/time formats are language agnostic.
  • 结果的语言:从用户角度出发,需要考虑的是,应对函数的结果使用何种语言。The language of the result: For users, what language should be used in the result of the function? 必须使用应用用户的语言显示月份名称和星期几。Names for months and weekdays need to be in the appropriate language for the user of the app. 这需要通过 Text 函数的第三个可选参数进行处理。This is handled with a third optional argument to the Text function.

无论是哪条信息,语言都是通过语言标记进行提供。For both, the language is provided with a language tag. 若要查看支持的语言列表,请在编辑栏或高级视图中键入 Text( 1234, "", ),然后滚动查看为第三个参数推荐的区域设置列表。To see the list of supported languages type Text( 1234, "", ) in the formula bar or advanced view and scroll through the list of locales suggested for the third argument.

自定义格式语言占位符Custom format language placeholder

若要指定自定义格式的语言,请使用:To specify the language of the custom format, use:

占位符Placeholder 说明Description
[$-LanguageTag][$-LanguageTag] LanguageTagLanguage 函数返回的语言标记。LanguageTag is a language tag as returned from the Language function. 语言标记的形式可以是只包含语言,如 [$-en] 表示英语;也可以是包含区域,如 [$-en-GB] 进一步指定英国。It can be in the form of just the language such as [$-en] for English, or it can also include the region such as [$-en-GB] to further specify Great Britain.

语言占位符可以显示在自定义格式中的任意位置,但只能出现一次。The language placeholder can appear anywhere in the custom format but only once.

如果你在编写公式时未提供语言占位符且从全局角度来看格式字符串不明确,那么创作工具会自动插入当前语言的语言标记。While writing a formula, if you do not provide a language placeholder and the format string is ambiguous from a global standpoint, the authoring tool will automatically insert the language tag for your current language.

如果在应用运行时未找到此占位符,则假定是 [$-en-US][$-en-US] is assumed if this placeholder is not present when your app is run.

备注

在今后推出的版本中,此占位符的语法可能会有所变化,以免与 Excel 支持的其他类似占位符混淆。In a future version, the syntax of this placeholder may change to avoid confusion with a similar, but different, placeholder supported by Excel.

结果语言标记Result language tag

Text 结果包含月份、星期几和 AM/PM 标记的已翻译字符串,以及相应的组分隔符和十进制分隔符。Appearing in the result of Text are translated strings for month, weekday, and AM/PM designations, as well as the appropriate group and decimal separators.

默认情况下,Text 使用应用用户的语言。By default, Text uses the language of the user running the app. Language 函数返回当前用户的语言标记。The Language function returns the language tag for the current user. 可以为 Text 的第三个可选参数提供语言标记,从而替代此默认设置。You can override this default by supplying a language tag for the optional third argument to Text.

语法Syntax

Text( Number, DateTimeFormatEnum [, ResultLanguageTag ] )Text( Number, DateTimeFormatEnum [, ResultLanguageTag ] )

  • Number - 必需。Number - Required. 要设置格式的数字或日期/时间值。The number or the date/time value to format.
  • DateTimeFormat - 必需。DateTimeFormat - Required. DateTimeFormat 枚举的成员。A member of the DateTimeFormat enumeration.
  • ResultLanguageTag - 可选。ResultLanguageTag - Optional. 要对结果文本使用的语言标记。The language tag to use for the result text. 默认使用当前用户的语言。By default, the language of the current user is used.

Text( Number, CustomFormat [, ResultLanguageTag ] )Text( Number, CustomFormat [, ResultLanguageTag ] )

  • Number - 必需。Number - Required. 要设置格式的数字或日期/时间值。The number or the date/time value to format.
  • CustomFormat - 必需。CustomFormat - Required. 用双引号括起的一个或多个占位符。One or more placeholders enclosed in double quotation marks.
  • ResultLanguageTag - 可选。ResultLanguageTag - Optional. 要对结果文本使用的语言标记。The language tag to use for the result text. 默认使用当前用户的语言。By default, the language of the current user is used.

示例Examples

运行这些公式的用户位于美国,且已选择英语作为其语言。The user running these formulas is located in the United States and has selected English as their language. Language 函数返回的是“en-US”。The Language function is returning "en-US".

NumberNumber

公式Formula 说明Description 结果Result
Text( 1234.59, "####.#" )Text( 1234.59, "####.#" ) 将数字格式设置为包含一个小数位。Formats the number with one decimal place. "1234.6""1234.6"
Text( 8.9, "#.000" )Text( 8.9, "#.000" ) 根据需要,为数字的小数部分补充结尾零。Pads the decimal portion of the number with trailing zeros, if needed. "8.900""8.900"
Text( 0.631, "0.#" )Text( 0.631, "0.#" ) 根据需要,为数字的整个部分补充前导零。Pads the whole portion of the number with leading zeros, if needed. "0.6""0.6"
Text( 12, "#.0#" )Text( 12, "#.0#" )
Text( 1234.568, "#.0#" )Text( 1234.568, "#.0#" )
为数字的小数部分补充一个小数位的零,并包含第二个小数位(若有)。Pads the decimal portion of the number with zeros for one decimal place, and includes a second decimal place if supplied. "12.0""12.0"
"1234.57""1234.57"
Text( 12000, "$ #,###" )Text( 12000, "$ #,###" )
Text( 1200000, "$ #,###" )Text( 1200000, "$ #,###" )
每三位数添加一个千位分隔符,并添加货币符号。Places a thousands separator every three digits, and includes a currency symbol. "$ 12,000""$ 12,000"
"$ 1,200,000""$ 1,200,000"

日期/时间Date/Time

  • 2015 年 11 月 23 日(星期一)2:37:47 PMAt 2:37:47 PM on Monday, November 23, 2015
  • 美国太平洋时区 (UTC-8)United States Pacific Time Zone (UTC-8)
公式Formula 说明Description 结果Result
Text( Now(), DateTimeFormat.LongDate )Text( Now(), DateTimeFormat.LongDate ) 设置为长日期字符串格式,采用当前用户的语言和区域设置。Formats as a long date string, in the language and locale of the current user. "Monday, November 23, 2015""Monday, November 23, 2015"
Text( Now(), DateTimeFormat.LongDateTime )Text( Now(), DateTimeFormat.LongDateTime ) 设置为长日期和时间字符串格式,采用当前用户的语言和区域设置,使用 12 小时制。Formats as a long date and time string, in the language and locale of the current user, using a 12-hour clock. "Monday, November 23, 2015 2:37:47 PM""Monday, November 23, 2015 2:37:47 PM"
Text( Now(), DateTimeFormat.LongTime24 )Text( Now(), DateTimeFormat.LongTime24 ) 设置为长时间字符串格式,使用 24 小时制。Formats as a long time string, using a 24-hour clock. "14:37:47""14:37:47"
Text( Now(), DateTimeFormat.ShortDate )Text( Now(), DateTimeFormat.ShortDate ) 设置为短日期字符串格式,采用当前用户的语言和区域设置。Formats as a short date string, in the language and locale of the current user. "11/23/2015""11/23/2015"
Text( Now(), "d-mmm-yy" )Text( Now(), "d-mmm-yy" ) 使用占位符的格式:Formats using placeholder characters:
  • d 表示相应月份中的多少号,一位数或两位数d for a single-digit or double-digit day of the month
  • - 是复制到结果中的文本字符- as a literal character copied to the result
  • mmm 表示三个字母的月份缩写mmm for a three-letter abbreviation of the month
  • - 是另一个复制到结果中的文本字符- as another literal character copied to the result
  • yy 表示两位数的年份缩写yy for a two-digit abbreviation of the year
"23-Nov-15""23-Nov-15"

全局应用Global apps

公式Formula 说明Description 结果Result
Text( 1234567.89, "[$-en-US]$ #,###" )Text( 1234567.89, "[$-en-US]$ #,###" ) , 解读为每三个字符之间的组分隔符,并将 $ 解读为货币符号。Interprets , as a grouping separator placed every three characters and $ as the currency symbol. 由于不会显示任何小数位,因此值会四舍五入到整数位。As no decimals are to be displayed, the value is rounded up to the next higher whole number. 在此示例中,[$-en-US] 是可选的,因为它是默认设置。The [$-en-US] is optional in this case, as this is the default. "$ 1,234,568""$ 1,234,568"
Text( 1234567.89, "[$-es-ES]€ #,###" )Text( 1234567.89, "[$-es-ES]€ #,###" ) , 解读为十进制分隔符,并将 解读为货币符号。Interprets , as a decimal separator and as the currency symbol. 由于 [$-fr-FR] 仅决定了格式字符串的解读方式,因此结果将使用通过默认的“en-US”语言标记进行解读的字符:将 .Because the [$-fr-FR] only determines how the format string is interpreted, the result will use the characters from the default "en-US" lanugage tag: . (句点)解读为十进制分隔符,并将 $ 解读为货币符号。(period) for decimal separator and $ for currency symbol. "$ 1234567.89""$ 1234567.89"
Text( 1234567.89, "[$-es-ES]€ #,###", "es-ES" )Text( 1234567.89, "[$-es-ES]€ #,###", "es-ES" ) , 解读为十进制分隔符。Interprets , as a decimal separator. 结果语言标记已设置为“fr-FR”。这样一来,将 ,(逗号)解读为十进制分隔符,并将 解读为货币符号。The result language tag has been set to "fr-FR" which will result in , (comma) being used as the decimal separator and as the currency symbol. "€ 1234567,89""€ 1234567,89"
Text( Date(2016,1,31), "dddd mmmm d" )Text( Date(2016,1,31), "dddd mmmm d" ) 以当前用户的语言返回星期几、月份和相应月份中的多少号。Returns the weekday, month, and day of the month in the language of the current user. 由于所有占位符都与语言无关,因此无需使用格式文本语言标记。Because none of the placeholders are language dependent, there is no need for a format text language tag. "Saturday January 31""Saturday January 31"
Text( Date(2016,1,31), "dddd mmmm d", "es-ES" )Text( Date(2016,1,31), "dddd mmmm d", "es-ES" ) 以“es-ES”语言返回星期几、月份和相应月份中的多少号。Returns the weekday, month, and day of the month in the "es-ES" language. "domingo enero 31""domingo enero 31"