使用ColdFusion重现这种printf格式的最直接的方法是什么? [英] What is the most direct way to reproduce this printf-like format with ColdFusion?

查看:138
本文介绍了使用ColdFusion重现这种printf格式的最直接的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经投入ColdFusion做一个非常简单的任务。应用程序有一些逻辑显示帮助代码(让我们不介绍什么是帮助代码),但是,逻辑是buggy,需要修复。给定一个双字母代码,一个1-4位数字和另一个1-2位数字,我需要显示他们像 printf call会:

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

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

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).

这里有一些例子,%s%04d%02d

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


$ b b

但是,这是我第一次使用ColdFusion,我找不到像 printf sprintf 以格式化字符串。

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

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.

推荐答案

<cfset bigNumberPadded = NumberFormat(bigNumber,"0000")>
<cfset smallNumberPadded = NumberFormat(smallNumber,"00")>
<cfoutput>#letterCode##bigNumberPadded##smallNumberPadded#<cfoutput>

或者...正如bpanulla建议的,并由Leigh更正

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

这篇关于使用ColdFusion重现这种printf格式的最直接的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆