This was already discussed recently, here on julia-users, I'm trying to get in touch with Dahua Lin (author of Formatting.jl) to see about adding a simpler `sfmt` that would help with this).
On Thursday, June 18, 2015 at 10:13:46 AM UTC-4, Tom Breloff wrote: > > I wonder if what we really need is just some extra additions to > Formatting.jl (since I think this is the best place to keep standard > formatting calls). We could add fmt2, fmt3, etc which would be meant for > formatting floats to that precision. I suspect that's the most common use > of formatting. Additionally, just a shorter name than "generate_formatter" > might help adoption for non-standard formatting. If this makes sense to > people, I'll start an issue on github, and perhaps a PR as well. > > > julia> using Formatting > > julia> fmt2 = generate_formatter("%1.2f") > sprintf_JTEuMmY! (generic function with 1 method) > > julia> fmt3 = generate_formatter("%1.3f") > sprintf_JTEuM2Y! (generic function with 1 method) > > julia> @time fmt2(31231.345435245) > 55.763 milliseconds (33974 allocations: 1444 KB) > "31231.35" > > julia> @time fmt2(31231.345435245) > 13.573 microseconds (15 allocations: 608 bytes) > "31231.35" > > julia> @time fmt3(31231.345435245) > 11.193 milliseconds (5882 allocations: 254 KB) > "31231.345" > > julia> @time fmt3(31231.345435245) > 16.231 microseconds (15 allocations: 608 bytes) > "31231.345" > > > > > On Thursday, June 18, 2015 at 3:55:01 AM UTC-4, cormu...@mac.com wrote: >> >> You could use a type: >> >> julia> type Out >> n::Float64 >> end >> >> julia> function Base.show(io::IO, n::Out) >> print(io, "$(round(n.n, 2))") >> end >> show (generic function with 83 methods) >> >> then you can just use Out(x) whenever you want x rounded to 2 d.p. >> >> julia> for i in 0.7454539:1.5:5 >> println("i is $i and displayed as $(Out(i))") >> end >> i is 0.7454539 and displayed as 0.75 >> i is 2.2454539000000002 and displayed as 2.25 >> i is 3.7454539000000002 and displayed as 3.75 >> >>