gpt4 book ai didi

julia - 在 Julia 中插值时如何格式化字符串?

转载 作者:行者123 更新时间:2023-12-04 01:24:45 24 4
gpt4 key购买 nike

在 Python 3 中,我会

print_me = "Look at this significant figure formatted number: {:.2f}!".format(floating_point_number)
print(print_me)

或者
print_me = f"Look at this significant figure formatted number: {floating_point_number:.2f}!"
print(print_me)

在 Julia
print_me = "Look at this significant figure formatted number: $floating_point_number"
print(print_me)

但这会产生说
Look at this significant figure formatted number: 61.61616161616161

如何让 Julia 限制它显示的小数位数?请注意,据我所知,要打印的字符串的必要存储排除了使用 @printf 的可能性。宏。

这可行,但在风格上似乎不正确。
floating_point_number = round(floating_point_number,2)
print_me = "Look at this significant figure formatted number: $floating_point_number"
print(print_me)

最佳答案

除了@niczky12 的回答,您还可以使用Formatting专为这类东西设计的包装!

Pkg.add("Formatting")
using Formatting: printfmt
x = 1.77715
printfmt("I'm long: $x, but I'm alright: {:.2f}", x)

输出:
I'm long: 1.77715, but I'm alright: 1.78 

关于julia - 在 Julia 中插值时如何格式化字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37031133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com