You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Matt Juntunen <ma...@hotmail.com> on 2021/05/13 15:59:03 UTC

[text][geometry] DoubleFormats utility

Hello,

For the geometry IO work I did recently, I ended up creating a DoubleFormats [1] utility class that generates formatters for various floating point string representations. It creates formatters for plain (straight decimal), scientific, engineering, and mixed plain and scientific formats. A maximum number of significant decimal digits and a minimum exponent value can be specified for each format. Here is a simple example:


int maxPrecision = 4;

DoubleFormat plain = DoubleFormats.createPlain(maxPrecision);
plain.format(Math.PI); // 3.142
plain.format(-123456789); // -123500000.0
plain.format(1.23456789e-6); // 0.000001235

DoubleFormat eng = DoubleFormats.createEngineering(maxPrecision);
eng.format(Math.PI); // 3.142
eng.format(-123456789); // -123.5E6
eng.format(1.23456789e-6); // 1.235E-6


I currently have the methods returning a DoubleFormat functional interface but I plan on changing this to simply DoubleFunction<String>.

Is there any interest in moving this functionality to commons-text? I find it quite useful since it is thread-safe and much faster than using String.format or similar. I've actually created a class with much the same functionality for use in my day job. It would be nice if I could use something from commons for that.

Regards,
Matt J

[1] https://github.com/apache/commons-geometry/blob/master/commons-geometry-io-core/src/main/java/org/apache/commons/geometry/io/core/utils/DoubleFormats.java


Re: [text][geometry] DoubleFormats utility

Posted by Gilles Sadowski <gi...@gmail.com>.
Hi.

Le ven. 14 mai 2021 à 04:17, Matt Juntunen <ma...@hotmail.com> a écrit :
>
> Hello,
>
> Yes, the JDK definitely has number formatting capabilities. The class that I propose moving to text was designed specifically for data IO operations, where large numbers of doubles need to be serialized to strings in some standard, non-localized format. I was unable to find exactly what I wanted for this in the JDK, so I wrote my own class. The main advantages to this code are that the produced formatters are 1) completely thread-safe,

Out of curiosity, what are use-cases for this feature?

> 2) easy to use, 3) offer a range of formats, and 4) are at least as performant as BigDecimal and DecimalFormat.

As a concrete discussion point, it might be interesting to post benchmarks
comparisons.

Regards,
Gilles

>> [...]

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [text][geometry] DoubleFormats utility

Posted by Matt Juntunen <ma...@hotmail.com>.
Hello,

Yes, the JDK definitely has number formatting capabilities. The class that I propose moving to text was designed specifically for data IO operations, where large numbers of doubles need to be serialized to strings in some standard, non-localized format. I was unable to find exactly what I wanted for this in the JDK, so I wrote my own class. The main advantages to this code are that the produced formatters are 1) completely thread-safe, 2) easy to use, 3) offer a range of formats, and 4) are at least as performant as BigDecimal and DecimalFormat.

-Matt
________________________________
From: Benjamin Marwell <bm...@apache.org>
Sent: Thursday, May 13, 2021 1:37 PM
To: Commons Developers List <de...@commons.apache.org>
Subject: Re: [text][geometry] DoubleFormats utility

I'm not a commons committer, but I think it moved to Java 8, where you
already have the same functionality available in the JDK. NumberFormat and
DecimalFormat [1] will do the same already, if I am not mistaken.

Ben

1: https://www.baeldung.com/java-number-formatting




On Thu, 13 May 2021, 17:59 Matt Juntunen, <ma...@hotmail.com> wrote:

> Hello,
>
> For the geometry IO work I did recently, I ended up creating a
> DoubleFormats [1] utility class that generates formatters for various
> floating point string representations. It creates formatters for plain
> (straight decimal), scientific, engineering, and mixed plain and scientific
> formats. A maximum number of significant decimal digits and a minimum
> exponent value can be specified for each format. Here is a simple example:
>
>
> int maxPrecision = 4;
>
> DoubleFormat plain = DoubleFormats.createPlain(maxPrecision);
> plain.format(Math.PI); // 3.142
> plain.format(-123456789); // -123500000.0
> plain.format(1.23456789e-6); // 0.000001235
>
> DoubleFormat eng = DoubleFormats.createEngineering(maxPrecision);
> eng.format(Math.PI); // 3.142
> eng.format(-123456789); // -123.5E6
> eng.format(1.23456789e-6); // 1.235E-6
>
>
> I currently have the methods returning a DoubleFormat functional interface
> but I plan on changing this to simply DoubleFunction<String>.
>
> Is there any interest in moving this functionality to commons-text? I find
> it quite useful since it is thread-safe and much faster than using
> String.format or similar. I've actually created a class with much the same
> functionality for use in my day job. It would be nice if I could use
> something from commons for that.
>
> Regards,
> Matt J
>
> [1]
> https://github.com/apache/commons-geometry/blob/master/commons-geometry-io-core/src/main/java/org/apache/commons/geometry/io/core/utils/DoubleFormats.java
>
>

Re: [text][geometry] DoubleFormats utility

Posted by Benjamin Marwell <bm...@apache.org>.
I'm not a commons committer, but I think it moved to Java 8, where you
already have the same functionality available in the JDK. NumberFormat and
DecimalFormat [1] will do the same already, if I am not mistaken.

Ben

1: https://www.baeldung.com/java-number-formatting




On Thu, 13 May 2021, 17:59 Matt Juntunen, <ma...@hotmail.com> wrote:

> Hello,
>
> For the geometry IO work I did recently, I ended up creating a
> DoubleFormats [1] utility class that generates formatters for various
> floating point string representations. It creates formatters for plain
> (straight decimal), scientific, engineering, and mixed plain and scientific
> formats. A maximum number of significant decimal digits and a minimum
> exponent value can be specified for each format. Here is a simple example:
>
>
> int maxPrecision = 4;
>
> DoubleFormat plain = DoubleFormats.createPlain(maxPrecision);
> plain.format(Math.PI); // 3.142
> plain.format(-123456789); // -123500000.0
> plain.format(1.23456789e-6); // 0.000001235
>
> DoubleFormat eng = DoubleFormats.createEngineering(maxPrecision);
> eng.format(Math.PI); // 3.142
> eng.format(-123456789); // -123.5E6
> eng.format(1.23456789e-6); // 1.235E-6
>
>
> I currently have the methods returning a DoubleFormat functional interface
> but I plan on changing this to simply DoubleFunction<String>.
>
> Is there any interest in moving this functionality to commons-text? I find
> it quite useful since it is thread-safe and much faster than using
> String.format or similar. I've actually created a class with much the same
> functionality for use in my day job. It would be nice if I could use
> something from commons for that.
>
> Regards,
> Matt J
>
> [1]
> https://github.com/apache/commons-geometry/blob/master/commons-geometry-io-core/src/main/java/org/apache/commons/geometry/io/core/utils/DoubleFormats.java
>
>