Converting a double number into a string (not printing!)

I am trying to get a string with a number (double) formatted with 2 decimals… If i use interpolation I get the following
A = 3.14256789
c = “The letter is $A”
c is then “The letter is 3.14256789”…

What I am looking for is “The letter is 3.14”.

If I use the printf package I can print what I am looking for, but not able to store it as variable.
@printf(“the letter is %.2f”, A)

Any solution to this?

I usually just use round(A, digits = 2) for this (or round(A, RoundDown, digits = 2) if you want to cut off the remaning digits rather than round up if the third digit is >= 0.5)

Thanks … Its a workaround… But if if you want @printf(“the letter is %3.f2”, A)… how would you do it?

Use @sprintf:

julia> x = @sprintf("the letter is %3.f2", A)
"the letter is   32"

julia> x
"the letter is   32"
1 Like

Thanks… That’s what I was looking for