使用 ColdFusion 重现这种类似 printf 的格式的最直接方法是什么?

发布于 2024-10-19 00:53:14 字数 770 浏览 4 评论 0原文

我因为一项非常简单的任务而被投入 ColdFusion。该应用程序有一些显示“帮助代码”的逻辑(我们不讨论什么是帮助代码),但是,该逻辑有缺陷,需要修复。给定一个两个字母的代码、一个 1-4 位数字和另一个 1-2 位数字,我需要像这个 printf 调用一样显示它们:

printf("%s%04d%02d", letterCode, bigNumber, smallNumber);

如果您不熟悉 printf函数中,它接受一个格式字符串(第一个参数),并根据给定的格式在其中写入其他变量。 %s 表示“写入字符串”,%d 表示“写入数字”; %0zd 表示“写入一个数字并用零填充,使其长度至少为 z 个字符(因此 %04d 表示“写入一个数字并用零填充)用零填充,使其长度至少为 4 位)。

以下是 %s%04d%02d 的一些示例:

"AD", 45, 12:  AD004512
"GI", 5121, 1: GI512101
"FO", 1, 0:    FO000100

但是,这是我第一次使用 ColdFusion,我找不到类似 printf的内容sprintf 格式化字符串。

另一个不再在这里工作的人诉诸了(非工作)循环,我认为最好使用库代码而不是实际修复循环,因为无论如何我可能需要再次做类似的事情。

I've been thrown into ColdFusion for a very simple assignment. The application has some logic to display "help codes" (let's not get into what is a help code), however, the logic is buggy and needs to be fixed. Given a two-letters code, a 1-4 digits number, and another 1-2 digits number, I would need to display them like this printf call would:

printf("%s%04d%02d", letterCode, bigNumber, smallNumber);

If you're not familiar with the printf function, it accepts a format string (the first parameter), and writes the other variables in it according to the given format. %s means "write a string" and %d means "write a number"; %0zd means "write a number and pad it with zeroes so it's at least z characters long (so %04d means "write a number and pad it with zeroes so it lengths at least 4 digits).

Here are a few examples with %s%04d%02d:

"AD", 45, 12:  AD004512
"GI", 5121, 1: GI512101
"FO", 1, 0:    FO000100

However, it's my very first time with ColdFusion, and I couldn't find anything like printf or sprintf to format strings.

The other guy, who doesn't work here anymore, resorted to a (non-working) loop, and I thought it would be better to use library code instead of actually fixing the loop, since anyways I might need to do similar things again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫腻 2024-10-26 00:53:14
<cfset bigNumberPadded = NumberFormat(bigNumber,"0000")>
<cfset smallNumberPadded = NumberFormat(smallNumber,"00")>
<cfoutput>#letterCode##bigNumberPadded##smallNumberPadded#<cfoutput>

或者...按照 bpanulla 的建议,并由 Leigh 纠正

<cfset args = ["AD", javacast("int", 45), javacast("int", 12)]>
<cfset output= createObject("java","java.lang.String").format("%s%04d%02d", args) >
<cfset bigNumberPadded = NumberFormat(bigNumber,"0000")>
<cfset smallNumberPadded = NumberFormat(smallNumber,"00")>
<cfoutput>#letterCode##bigNumberPadded##smallNumberPadded#<cfoutput>

Or alternatively... as suggested by bpanulla, and corrected by Leigh

<cfset args = ["AD", javacast("int", 45), javacast("int", 12)]>
<cfset output= createObject("java","java.lang.String").format("%s%04d%02d", args) >
甜扑 2024-10-26 00:53:14

您可以使用 NumberFormat 在 CF 中用前导零填充数字。

<cfoutput>#letterCode##NumberFormat(bigNumber, '0000')##NumberFormat(smallNumber, '00')#</cfoutput>

You can use NumberFormat to pad a number with leading zeros in CF.

<cfoutput>#letterCode##NumberFormat(bigNumber, '0000')##NumberFormat(smallNumber, '00')#</cfoutput>
著墨染雨君画夕 2024-10-26 00:53:14

在支持 ColdFusion 的 Java 层中有很多方法可以做到这一点。以下是一个 Java 资源:

http://download。 oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

使用 CFOBJECT 或 CreateObject 创建所需的 Java 类的实例。

There's lots of ways to do this in the Java layer underpinning ColdFusion. Here's one Java resource:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

Make instances of the Java classes you need with CFOBJECT or CreateObject.

薄荷→糖丶微凉 2024-10-26 00:53:14

我假设您正在将这些显示到网页上?如果是这样,我会使用 switch/case 语句。既然你说“给定两个字母的代码......”,开关/外壳就可以很好地工作。例如:

<cfswitch expression="#twoLetterCode#">
    <cfcase value="aa12348">%s%04d%02d</cfcase>
    <cfcase value="bb23456">%s%05f%01e</cfcase>
    <cfcase value="cc97641">%s%08g%10j</cfcase>
    <cfdefaultcase>%s%04d%02d</cfdefaultcase>
</cfswitch>

或者您可以使用 if/else 代替。但要点(回答你的问题)是在 ColdFusion 中你只需键入显示字符(无论是帮助代码、文本还是其他)。您不需要使用特殊函数来向页面显示文本。

I am assuming you are displaying these to a webpage? If so, I would use a switch/case statement. Since you said "given a two-letter code...", a switch/case would work well. For example:

<cfswitch expression="#twoLetterCode#">
    <cfcase value="aa12348">%s%04d%02d</cfcase>
    <cfcase value="bb23456">%s%05f%01e</cfcase>
    <cfcase value="cc97641">%s%08g%10j</cfcase>
    <cfdefaultcase>%s%04d%02d</cfdefaultcase>
</cfswitch>

Or you could use an if/else instead. But the main point (to answer your question) is that in ColdFusion you just type out the display characters (be it help codes, or text, or whatever). You don't need to use a special function to display text to the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文