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

Number to Text conversion #218

Open
dschrempf opened this issue Feb 12, 2018 · 7 comments
Open

Number to Text conversion #218

dschrempf opened this issue Feb 12, 2018 · 7 comments

Comments

@dschrempf
Copy link

Hi,

may I suggest adding conversion functions such as

import qualified Data.Text as T
import qualified Data.Text.Lazy as T (toStrict)
import qualified Data.Text.Lazy.Builder as B
import qualified Data.Text.Lazy.Builder.Int as B
import qualified Data.Text.Lazy.Builder.RealFloat as B

realFloatToText :: RealFloat a => a -> T.Text
realFloatToText = T.toStrict . B.toLazyText . B.realFloat

intToText :: Integral a => a -> T.Text
intToText = T.toStrict . B.toLazyText . B.decimal

to Data.Text because I spent now around two hours in finding a way to convert numbers to Text objects. Maybe there is a better way, in that case it would be nice to have it documented in the main file of the module.

Thank you!

@Fuuzetsu
Copy link
Member

@robwithhair
Copy link

+1 very confusing not having the fastest solution as part of the Text package. Please consider adding or directing readers of documentation in the right direction.

@Anton-Latukha
Copy link

Anton-Latukha commented Jan 30, 2021

Agree.

I think there is no debate there should be a fast function to convert: Integral a => a -> Text at least.
Very surprised the fastest ways in the default library are manually through Lazy Builder, then assemble it, then additionally run O(n) toStrict on it, it is, well, a bit excessive.

There is: text-builder where is decimal :: Integral a => a -> Builder.
Good if that package would've been upstreamed here.

@jkaye2012
Copy link

Is this request under consideration? I was surprised to find that this does not exist yet.

@Bodigrim
Copy link
Contributor

Float-to-string conversion is hard. Once haskell/bytestring#365 lands into bytestring and UTF8-based text-2.0 is released, the fastest way will be to create a ByteString and convert it to Text.

@BebeSparkelSparkel
Copy link
Contributor

BebeSparkelSparkel commented Jan 22, 2024

@Bodigrim Currently, the haskell/bytestring#365 has been pulled but, I have recently updated the float to string implementation in bytestring and have greatly improved its performance and added some capabilities. However, I have not made any breaking changes to the interface and integration to text can begin now.

The performance and organization PRs currently submitted

New formatting options

Also, the Ryu algorithm may not print floating point numbers the same as your current implementation since it minimizes the the number of digits printed. I have not looked at the text implementation so this may not be an issue.

@Bodigrim
Copy link
Contributor

Thanks @BebeSparkelSparkel. I'm severely overloaded at the moment, but I'm sure @clyring will review your PRs for bytestring. Feel free to ping me in a couple of weeks though.

A PR migrating text to use bytestring for double-to-string conversion would be welcome.

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

8 participants