Set precision when printing to file

1,114 views
Skip to first unread message

Robert DJ

unread,
Jun 15, 2015, 9:50:17 AM6/15/15
to julia...@googlegroups.com
Hi,

I would like to write floating point numbers to a file and limit the number of digits/decimals. With e.g.

f = open("test.txt", "w")
println
(f, 1/3)
close
(f)

test.txt contains 0.3333333333333333 and I would like it to be only 0.33.

Is there a way to do this?

Thanks,

Robert

Message has been deleted

Huda Nassar

unread,
Jun 15, 2015, 10:37:35 AM6/15/15
to julia...@googlegroups.com
julia> f = open("test2.txt","w")
IOStream(<file test2.txt>)
julia
> @printf(f,"%0.2f",1/3)
julia
> close(f)

This should do the job

Robert DJ

unread,
Jun 17, 2015, 1:27:08 PM6/17/15
to julia...@googlegroups.com
It certainly does -- thanks a lot!

cormu...@mac.com

unread,
Jun 18, 2015, 3:55:01 AM6/18/15
to julia...@googlegroups.com
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

Tom Breloff

unread,
Jun 18, 2015, 10:13:46 AM6/18/15
to julia...@googlegroups.com
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"

Scott Jones

unread,
Jun 18, 2015, 10:20:08 AM6/18/15
to julia...@googlegroups.com
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).

Tom Breloff

unread,
Jun 18, 2015, 10:30:05 AM6/18/15
to julia...@googlegroups.com
Scott: I remember there being another discussion but I can't seem to find it.  How did you try to get in touch?  Do you want to start a github issue and I'll comment there?

Simon Byrne

unread,
Jun 18, 2015, 11:15:05 AM6/18/15
to julia...@googlegroups.com

Scott Jones

unread,
Jun 18, 2015, 11:43:31 AM6/18/15
to julia...@googlegroups.com
Ah, I forgot, it wasn't in julia-users, it was in julia-dev!

Scott Jones

unread,
Jun 18, 2015, 11:44:35 AM6/18/15
to julia...@googlegroups.com
I tried directly e-mailing him (after having gotten a lot of grief for posting so much!), but I haven't heard back...

Stefan Karpinski

unread,
Jun 18, 2015, 1:00:20 PM6/18/15
to Julia Users
Dahua is pretty busy and lives in a very different time zone. He also may not want to respond to direct messages.

Scott Jones

unread,
Jun 18, 2015, 1:07:21 PM6/18/15
to julia...@googlegroups.com

I was aware that it could be any or all of those issues.  I was just stating the facts, not complaining.

Here is what I sent him:
Hi, I hope you don’t mind a direct e-mail.
I was pointed at your Formatting.jl package, after I’d made the suggestion of adding a fmt function in a new package,
and kmsquire pointed out that there was already a Formatting.jl package, which is where it should go, and then
when I investigated, I saw that it had a fmt function that was almost exactly what I was suggesting.
I was wondering what you’d think of me adding my idea (renamed possibly `sfmt` to avoid the naming conflict`) to your very
nice package (which I think should be in Base, instead of so much *unused* stuff like “RopeString” ;-) ).
My idea was the following:
sfmt( value [, format [, format arguments] ] )
The reason to have the value first, is that you could have methods added to have a default format for specific types.
For example, the C sprintf(buf, “%*s”, 8, string) would become something like:
sfmt( string, “*s”, 8)
What do you think?
Thanks, Scott


As I said, his formatting package does seem very nice, and very complete as well...
[OT: Will he be coming to JuliaCon?  I hope so!]

Tom Breloff

unread,
Jun 18, 2015, 1:22:04 PM6/18/15
to julia...@googlegroups.com
Yes it was on julia-dev.  Regardless I think the right path forward is to start a discussion on github (in Formatting.jl) and we could hash out exactly what would be nice for users.  Based on your posts, you may have a very different perspective on string formatting that your average developer/scientist.  My goal would be to have methods that are extremely simple to understand and super short to write, so that people actually use them.  Bonus points if those methods are flexible enough for those that need additional options.  Anyways... over to github?

Scott Jones

unread,
Jun 18, 2015, 2:08:45 PM6/18/15
to julia...@googlegroups.com
Waiting for your comments, over at https://github.com/lindahua/Formatting.jl/issues/8!

Tom Breloff

unread,
Jun 22, 2015, 2:53:58 PM6/22/15
to julia...@googlegroups.com
For anyone else that's curious, Scott and I have been discussing this on github, and would love input from others.  I have a minimal working example with a new "fmt" method, but I'd like to work on something that people will actually use.

Scott Jones

unread,
Jun 22, 2015, 4:30:43 PM6/22/15
to julia...@googlegroups.com
Yes, Tom is making me feel lazy, by all that he's gotten done already!  Of course, the more input the better, please join us!
Reply all
Reply to author
Forward
0 new messages