Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to format float64 to_s? #2220

Closed
ray-delossantos opened this issue Feb 24, 2016 · 6 comments
Closed

How to format float64 to_s? #2220

ray-delossantos opened this issue Feb 24, 2016 · 6 comments

Comments

@ray-delossantos
Copy link

Hello:

In the next code

test = 9525365.25
puts test.to_s => 9.52537e+06

I would like to have a string with this format => "9525365.25"

@jhass
Copy link
Member

jhass commented Feb 24, 2016

Just use String#%:

test = 9525365.25
puts "%.2f" % test

https://carc.in/#/r/sz5

Please consider using StackOverflow for such questions, thanks!

@jhass jhass closed this as completed Feb 24, 2016
@ray-delossantos
Copy link
Author

Sorry for the misuse of the crystal github issues module. Thanks for your help.

@ysbaddaden
Copy link
Contributor

I find the default Float#to_s to be rather annoying and I would love if it returned nice numbers when it can. Ruby does a better job here.

@asterite
Copy link
Member

@ysbaddaden Me too. We just need to port this from C to Crystal ;-)

@asterite
Copy link
Member

Or we could just use it and have a wrapper for it.

@ysbaddaden
Copy link
Contributor

I found an alternative to Dragon4: Grisu3 by Florian Loitsch which is expected to be faster yet correct in 99.5% of cases. It's apparently used in V8 and by Mozilla, and a C library is available: https://github.com/google/double-conversion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants