在 SWI-Prolog 中写入格式化文本

发布于 2024-12-08 06:43:56 字数 638 浏览 0 评论 0原文

我在 SWI-Prolog 文档上花费了大量时间,但一无所获。我的愿望是能够格式化输出的数字,以便我控制显示的总小数位数,并在给定的字符宽度中右对齐数字。例如:

  2.500   (trailing zeroes displayed)
 34.432   (rounded from a much longer decimal value)
213.110

所有 3 个都在 7 个字符宽的空间中右对齐,显示 3 位小数(即使这些数字为零)。我可以单独完成其中一些事情,但不能一次全部完成。

writef( '%7R', [34.342]).
writef( '%7R', [34.300]).

^^^ 这非常接近我想要的,但不幸的是它确实显示任何尾随零(它总是会忽略它们)。另外,在将舍入值传递给 writef() 之前,我必须手动进行舍入。

format( '~3f', 34.34219089).
format( '~3f', 1234.3).

这个进行舍入,并允许尾随零,但我找不到使用“格式”函数强制右对齐的方法,而且我找不到一种将 writef (对齐)功能与格式(舍入和零显示)。

有什么想法吗?

非常感谢!

I have spent a significant amount of time in the SWI-Prolog documentation and am getting nowhere. My desire is to be able to format numbers that are output such that I am controlling total # decimal digits displayed and also right aligning the numbers in a given character width. For example:

  2.500   (trailing zeroes displayed)
 34.432   (rounded from a much longer decimal value)
213.110

All 3 are right aligned in a 7 character wide space, with 3 decimal places displayed (even when those are zero). I can accomplish some of these things individually, but not all at once.

writef( '%7R', [34.342]).
writef( '%7R', [34.300]).

^^^ This comes very close to what I want, but unfortunately it does display any trailing zeroes (it will always omit them). Also, I have to do the rounding manually before passing the rounded value to writef().

format( '~3f', 34.34219089).
format( '~3f', 1234.3).

This one does the rounding, and allows trailing zeroes, but I can find no way to force right alignment using the "format" function, and I can't find a way to combine the functionality of writef (alignment) with format (rounding and zero display).

Any ideas?

Thanks much!

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

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

发布评论

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

评论(1

一个人练习一个人 2024-12-15 06:43:56

我遇到了同样的问题,甚至更多:

[debug]  ?- format( '~3f', 34.34219089).
34,342

逗号(尽管语言环境需要,但请注意这一点)使读回输出变得复杂。我最终得到了一些丑陋的解决方法来控制舍入:

[debug]  ?- X is round(34.34219089 * 1000) / 1000, write(X).
34.342
X = 34.342.

要填充和对齐,您应该使用制表位,由成对的 t| 控制。文档在这个主题上有点过于综合。例如,要以电子表格默认样式(文本左对齐,数字右对齐)打印数字表:

test(indent) :- nl,
    forall(member(L, [[a,    3.66,      55.5334],
              [basd, 22.876345, 2113.4465],
              [cas,  0.6623233, 53.5]
             ]),
           format('~s~t~20|~t~3f~40|~t~3f~60|~n', L)).

请注意“空间分配器”说明符 ~t 的位置、绝对“列宽度”~|,考虑字段类型说明符。输出:

?- run_tests(sheet_inventory:indent).
% PL-Unit: sheet_inventory:indent 
a                                  3,660              55,533
basd                              22,876            2113,447
cas                                0,662              53,500

I got the very same problem, and more:

[debug]  ?- format( '~3f', 34.34219089).
34,342

The comma (albeit required by locale, watch out for this) complicates reading back the output. I ended up with some ugly workaround to control rounding:

[debug]  ?- X is round(34.34219089 * 1000) / 1000, write(X).
34.342
X = 34.342.

To pad and align you should use tab stops, controlled by pairs of t and |. Documentation it's a bit too much synthetic on this topic. For instance, to print a table of numbers in spreadsheet default style (text left align, number right align):

test(indent) :- nl,
    forall(member(L, [[a,    3.66,      55.5334],
              [basd, 22.876345, 2113.4465],
              [cas,  0.6623233, 53.5]
             ]),
           format('~s~t~20|~t~3f~40|~t~3f~60|~n', L)).

Note the position of 'space allocator' specifier ~t, the absolute 'column width' ~|, regards the field type specifier. The output:

?- run_tests(sheet_inventory:indent).
% PL-Unit: sheet_inventory:indent 
a                                  3,660              55,533
basd                              22,876            2113,447
cas                                0,662              53,500
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文