Mathematica 中的 Sprintf 等价物? [英] Sprintf equivalent in Mathematica?

查看:50
本文介绍了Mathematica 中的 Sprintf 等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么维基百科将 Mathematica 列为带有 printf 的编程语言.我只是在 Mathematica 中找不到等价物.

I don't know why Wikipedia lists Mathematica as a programming language with printf. I just couldn't find the equivalent in Mathematica.

我的具体任务是处理带有填充数字的数据文件列表,我曾经在 bash 中使用

My specific task is to process a list of data files with padded numbers, which I used to do it in bash with

fn=$(printf "filename_%05d" $n)

我在 Mathematica 中找到的最接近的函数是 PaddedForm.经过一些试验和错误,我得到了

The closest function I found in Mathematica is PaddedForm. And after some trial and error, I got it with

"filename_" <> PaddedForm[ Round@#, 4, NumberPadding -> {"0", ""} ]&

很奇怪,我必须使用数字 4 来获得类似于我从%05d"得到的结果.我完全不理解这种行为.有人可以给我解释一下吗?

It is very odd that I have to use the number 4 to get the result similar to what I get from "%05d". I don't understand this behavior at all. Can someone explain it to me?

这是实现我过去在 bash 中实现的最佳方式吗?

And is it the best way to achieve what I used to in bash?

推荐答案

我不会为此使用 PaddedForm.事实上,我不确定 PaddedForm 是否适用于任何事情.相反,我会使用旧的 ToStringCharactersPadLeft,如下所示:

I wouldn't use PaddedForm for this. In fact, I'm not sure that PaddedForm is good for much of anything. Instead, I'd use good old ToString, Characters and PadLeft, like so:

toFixedWidth[n_Integer, width_Integer] := 
  StringJoin[PadLeft[Characters[ToString[n]], width, "0"]]

然后你可以使用StringFormToString来制作你的文件名:

Then you can use StringForm and ToString to make your file name:

toNumberedFileName[n_Integer] :=
  ToString@StringForm["filename_``", toFixedWidth[n, 5]]

Mathematica 不太适合这种字符串处理.

Mathematica is not well-suited to this kind of string munging.

编辑添加: Mathematica 本身没有所需的功能,但 java.lang.String 类具有静态方法 format() 接受 printf 风格的参数.您可以很容易地使用 Mathematica 的 JLink 功能调用它.性能不会很好,但对于许多用例,您不会太在意:

EDIT to add: Mathematica proper doesn't have the required functionality, but the java.lang.String class has the static method format() which takes printf-style arguments. You can call out to it using Mathematica's JLink functionality pretty easily. The performance won't be very good, but for many use cases you just won't care that much:

Needs["JLink`"];
LoadJavaClass["java.lang.String"];
LoadJavaClass["java.util.Locale"];
sprintf[fmt_, args___] :=
 String`format[Locale`ENGLISH,fmt,
  MakeJavaObject /@
   Replace[{args},
    {x_?NumericQ :> N@x,
     x : (_Real | _Integer | True | 
         False | _String | _?JavaObjectQ) :> x,
     x_ :> MakeJavaExpr[x]},
    {1}]]

您需要做更多的工作,因为 JLink 对带有可变数量参数的 Java 函数有点愚蠢.format() 方法接受一个格式字符串和一个 Java Object 数组,Mathematica 不会自动进行转换,这就是 MakeJavaObject 是为了.

You need to do a little more work, because JLink is a bit dumb about Java functions with a variable number of arguments. The format() method takes a format string and an array of Java Objects, and Mathematica won't do the conversion automatically, which is what the MakeJavaObject is there for.

这篇关于Mathematica 中的 Sprintf 等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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