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

Parsing strings with scientific notation #14

Open
samskeller opened this issue Apr 4, 2019 · 1 comment
Open

Parsing strings with scientific notation #14

samskeller opened this issue Apr 4, 2019 · 1 comment

Comments

@samskeller
Copy link

Hi there,

Right now, it doesn't seem like the valueOf() methods on the decimal4j classes are able to parse strings that are the scientific notation representation of numbers. For example, if I want to create a Decimal4f instance of the string representation of the number 0.0003, I'll try something like this:

double myNumber = 0.0003;
String myNumberString = String.valueOf(myNumber);
System.out.println(myNumberString); // prints "3.0E-4"
Decimal4f myNumberDecimal = new Decimal4f.valueOf(myNumberString)

I'll get a NumberFormatException error on that last line with the message of Cannot parse Decimal value with scale 4 for input string: "3.0E-4".

It'd be great to have the library accommodate scientific notation strings -- this is something BigDecimal does, so if we want to use decimal4j classes to parse strings, we have to first feed the string into a BigDecimal and then feed that into the decimal4j class.

Thanks!

@terzerm
Copy link
Member

terzerm commented Apr 7, 2019

Hi Sam,

Thanks for using decimal4j and for your enhancement request.

It is correct that decimal4j numbers do not currently support scientific notation in string representations. This is kind of by design because it is "fixed precision" and therefore the string representation is always fixed, too.

I do however acknowledge that it would be a good feature and we may add it in the future. For the time being I suppose you'd have to go the way via BigDecimal or Double.parse (the latter of course may involve precision issues and is therefore not optimal).

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

2 participants