在 SWI-Prolog 中写入格式化文本
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,甚至更多:
逗号(尽管语言环境需要,但请注意这一点)使读回输出变得复杂。我最终得到了一些丑陋的解决方法来控制舍入:
要填充和对齐,您应该使用制表位,由成对的
t
和|
控制。文档在这个主题上有点过于综合。例如,要以电子表格默认样式(文本左对齐,数字右对齐)打印数字表:请注意“空间分配器”说明符 ~t 的位置、绝对“列宽度”~|,考虑字段类型说明符。输出:
I got the very same problem, and more:
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:
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):Note the position of 'space allocator' specifier ~t, the absolute 'column width' ~|, regards the field type specifier. The output: