在Julia中,将逗号插入整数以进行打印,例如Python 3.6+ [英] In Julia, insert commas into integers for printing like Python 3.6+
问题描述
我想将逗号插入大整数以进行打印.
I want to insert commas into large integers for printing.
julia> println(123456789) # Some kind of flag/feature inserts commas.
"123,456,789"
在Python 3.6及更高版本中,这很容易做到:
In Python 3.6+ this is easy to do:
>>> print(f"{123456789:,d}")
123,456,789
但是,目前看来标准的Julia Julia print/println函数没有此功能.仅使用print/println函数可以做什么?
However, it does not appear that the standard Julia print/println functions have this feature at the present time. What can I do using just the print/println functions?
推荐答案
我想某些语言中最直接的方法是在printf
中使用'
格式修饰符.我茱莉亚,这看起来应该像这样:
I guess the most straightforward way in some languages would be to use the '
format modifier in printf
. I Julia this WOULD look like so:
using Printf # a stdlib that ships with julia which defines @printf
@printf "%'d" 12345678
但是,不幸的是,此标志尚不支持,因为您会从错误中看到:
However, unfortunately, this flag is not yet supported as you can see from the error you'll get:
julia> @printf "%'d" 12345678
ERROR: LoadError: printf format flag ' not yet supported
If you like this feature, maybe you should think about adding it to the Printf stdlib so that everyone would benefit from it. I don't know how difficult this would be though.
更新:请注意,尽管宏是在stdlib Printf中定义的,但以上错误却明确地在Base/printf.jl:48
中引发.我还在此处
UPDATE: Note that although the macro is defined in stdlib Printf, the error above is explicitly thrown in Base/printf.jl:48
. I also filed an issue here
这篇关于在Julia中,将逗号插入整数以进行打印,例如Python 3.6+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!