-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
BigDecimal's scale should be zero when all fractional digits are zero #81
Comments
Hi @aprat84,
I actually think that would make things more confusing. Depending on the application, one can expect var x = new BigDecimal("123.000000");
System.out.println(x.scale()); // 6
System.out.println(x.toString()); // 123.000000 If you want to compare $a = BigDecimal::of('2');
$b = BigDecimal::of('2.000');
$a->isEqualTo($b); // true If you want to strip trailing zeros, you can use $a = BigDecimal::of('123.456000'); // 123.456000
$b = $a->stripTrailingZeros(); // 123.456 You can actually compare string representations of two $a = BigDecimal::of('2')->stripTrailingZeros();
$b = BigDecimal::of('2.000')->stripTrailingZeros();
(string) $a === (string) $b; // true But again, I would strongly recommend to use the built-in comparison methods.
Maybe related: |
Well, everything you said makes sense, but I don't see why it works like this, though I'm no math expert 😅 Comparing to JAVA As I said, I'm not comparing two instances of |
When working with databases (MySQL8) and decimal columns, it returns a string with every fractional digit, even when all are zero.
In that case, when all fractional digits are zero,
BigDecimal
still considers them, and creates an scaled instance:In reality, both are the same exact number, so IMHO they should be represented equally when stringified.
Should the parser consider only significant fractional digits?
I mean, what is the real scale of
2.123000
, 3 or 6?What are your thoughts?
My use case is that I'm testing an XML generation, and printing on it
BigDecimal
s using(string)$value
, and it gives different results when using factories than when retrieving entities from DB.The text was updated successfully, but these errors were encountered: