How to format a float in annotate

I’m trying to annotate something in a plot but i’m unable to format the floats i’m working with. I’m trying to change the precision of the float, is it possible with annotate or do I need to do it outside the annotate function? Screenshot from 2021-10-19 19-46-57

plot(temperature, σ, label = "experimental", xlabel = "Temperatura (°C)", ylabel = "Tensão Superficial (dinas/cm)")
plot!(temp_fit, sup_tension(temp_fit, fit.param), label = "mq fit")
annotate!(75, 73, text("a = $a, b = $b", 8))

You can just do it within the string you’ve passed to text:

annotate!(75, 73, text("a = $(round(a, sigdigits=3)), b = $(round(b, sigdigits=4))", 8))

or with the Printf standard library,

annotate!(75, 73, text(@sprintf("a = %0.3f, b = %0.3f", a, b), 8))
4 Likes

This is what I used to round the floating point numbers

using Printf

function myrounddigits(x;digits=4)
    num=round(x,digits=digits)
    result = ""
    if digits==0
        result = @sprintf("%-10.0f",num)
    elseif digits==1
        result = @sprintf("%-10.1f",num)
    elseif digits==2
        result = @sprintf("%-10.2f",num)
    elseif digits==3
        result = @sprintf("%-10.3f",num)
    elseif digits==4
        result = @sprintf("%-10.4f",num)
    elseif digits==5
        result = @sprintf("%-10.5f",num)
    elseif digits==6
        result = @sprintf("%-10.6f",num)
    elseif digits==7
        result = @sprintf("%-10.7f",num)
    elseif digits==8
        result = @sprintf("%-10.8f",num)
    end
    return rstrip(result)
end

You need the rstrip to strip out all the blank spaces in the result string.

1 Like