haskell / text

Haskell library for space- and time-efficient operations over Unicode text.
http://hackage.haskell.org/package/text
BSD 2-Clause "Simplified" License
407 stars 159 forks source link

Number to Text conversion #218

Open dschrempf opened 6 years ago

dschrempf commented 6 years ago

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 commented 4 years ago

http://hackage.haskell.org/package/double-conversion-2.0.2.0 ?

robwithhair commented 4 years ago

+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 commented 3 years ago

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 commented 3 years ago

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

Bodigrim commented 3 years ago

Float-to-string conversion is hard. Once https://github.com/haskell/bytestring/pull/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 commented 10 months ago

@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 commented 10 months ago

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.

Bodigrim commented 2 months ago

I think it's time we add

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

because this incantation is so arcane that the vast majority of users succumb to T.pack . show, which is very inefficient.

I'm unsure about the name. It should mention that it's a decimal representation, not hexadecimal. Maybe simply Data.Text.decimal?..