In Python 3, I would
print_me = "Look at this significant figure formatted number: {:.2f}!".format(floating_point_number) print(print_me)
or
print_me = f"Look at this significant figure formatted number: {floating_point_number:.2f}!" print(print_me)
In Julia
print_me = "Look at this significant figure formatted number: $floating_point_number" print(print_me)
but this would yield say
Look at this significant figure formatted number: 61.61616161616161
How do I get Julia to restrict the number of decimal places it displays? Note that the necessary storage of the string to be printed, to my knowledge, rules out using the @printf
macro.
This works, but does not seem stylistically correct.
floating_point_number = round(floating_point_number,2) print_me = "Look at this significant figure formatted number: $floating_point_number" print(print_me)
Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.
To identify a string literal as an interpolated string, prepend it with the $ symbol. You can't have any white space between the $ and the " that starts a string literal.
jl package in Julia, which provides Python f-string functionality.
Julia uses printf() function similar to C, in order to format strings. Here, we print using the macro @printf. Characters: In the following code, we are storing a single letter 'c' in the variable named 'character'. The character variable is then printed using '%c' format specifier as its data type is char.
You can use @sprintf
macro from the standard library package Printf
. This returns a string rather than just printing it as @printf
.
using Printf x = 1.77715 print("I'm long: $x, but I'm alright: $(@sprintf("%.2f", x))")
Output:
I'm long: 1.77715, but I'm alright: 1.78
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With