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

SetString("3,60") returns a NaN instead of an error #153

Closed
bojanz opened this issue Jun 15, 2020 · 6 comments
Closed

SetString("3,60") returns a NaN instead of an error #153

bojanz opened this issue Jun 15, 2020 · 6 comments

Comments

@bojanz
Copy link

bojanz commented Jun 15, 2020

I've converted bojanz/currency from cockroachdb/apd to this package, and the tests are green:
bojanz/currency@2a46a5c

However, I was surprised by one difference in behavior between the two packages:

result, ok := new(decimal.Big).SetString(n)

If I pass "3,60" to SetString(), which is a string with an invalid decimal separator, instead of getting nil, false I get NaN, true.

This forces me to check for NaN every time:

if !ok || result.IsNaN(0) {
  return Amount{}, InvalidNumberError{"Amount.Mul", n}
}

Is this intentional, or a bug?

@ericlagergren
Copy link
Owner

Interesting! I assume NaN is incorrect, but I’ll need to double check the GDA spec first.

@ericlagergren
Copy link
Owner

The GDA spec says:

The result of attempting to convert a string which does not have the syntax of a numeric string is [0,qNaN].

http://speleotrove.com/decimal/daconvs.html#reftonum

Admittedly that’s unfortunate. Returning false sounds like it should be the correct behavior, even if it returns (NaN, false). I’ll see what I can do.

@Trial97
Copy link

Trial97 commented Feb 25, 2021

Hi @ericlagergren,

Is there any news regarding this?
Also is the NaN check intended after SetString to see if the decimal.Big was set correctly?

Thanks,
Trial97

@ericlagergren
Copy link
Owner

The NaN check is to see if the result is NaN, which is a valid GDA decimal.

But like I mentioned before, the library should be updated so that

SetString("invalid") // NaN, false
SetString("NaN") // NaN, true

Ultimately, though, you'd still have to check if the result is NaN. A user could pass "NaN" instead of "3,60".

@ericlagergren
Copy link
Owner

You'll want to check the conditions: #166 (comment)

@gedw99
Copy link

gedw99 commented Oct 23, 2021

@bojanz just adding myself here , after our chat on Reddit .

@ericlagergren
This looks amazing 🤩

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

No branches or pull requests

4 participants