Customize a floating type to display different precisions

Customize a floating type to display different precisions

Hello, everyone
I want to customize a floating type to display three decimal places, such as displaying 3.141

I have used the following sentence before
show(io::IO, e::Float32) = print(io, @sprintf(“%.2f”, e))

So the following has warning
const ff33 = Float32
show(io::IO, e::ff33) = print(io, @sprintf(“%.3f”, e))

primitive type ff33 <: AbstractFloat 32 end
This definition is ok, but the type conversion of ff33 and Float32 has to be processed

Is there the easiest way to customize the display of three decimal places?

Can someone help me with this?

Try

julia> round(pi, sigdigits=3)
3.14

Thank you
But I don’t want to change the value of the data, just want to display different decimal places

As suggested by @dpsanders, just do a round before each display of your number.

2 Likes