Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Bug: Fix precision for currency to use local specification #5672

Closed
wants to merge 1 commit into from

Conversation

park9140
Copy link

@park9140 park9140 commented Jan 7, 2014

Currency improperly forces the precision to 2 decimal places for all currency formmating, overriding the locale settings.

Pull request restores the intended functionality to apply the local based precision values.

@park9140
Copy link
Author

I may not have labeled this well enough initially. This is should be considered a bug in the implementation. All indicators show that decimal precision for locales are supported in angular, and yet this bug removes that feature.

@Juanmcuello
Copy link
Contributor

@park9140 , did you have any update on this? I'm interested in seeing this issue fixed.

@park9140
Copy link
Author

@Juanmcuello, this pull request is still fine as far as I know, but I haven't seen any response from any angular people.

@park9140
Copy link
Author

@petebacondarwin, you seem like someone associated with bugs. Is it possible this could get fixed or at least tagged correctly as a bug?

"lgSize": 3,
"macFrac": 0,
"maxFrac": 3,
"minFrac": 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the closure localization stuff actually give us these?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can ask formatNumber() to figure out the locale precision, because it's a static routine and doesn't have access to the locale info from ngLocale, it needs to be determined in the currency filter itself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp, yes, closure localization does provide the localization pattern information.

As for the the formatNumber() figuring out the locale precision.

Format number does not do this, the actual currency filter does. The locale provides the "pattern" object by which a number formatter is configured.

The main change here is to the currency filter, which was locking the precision by passing the fractionSize parameter of it's formatNumber call to 2.

The secondary change is that the formatNumber function was not utilizing the passed pattern maxFrac value when determining precision.

These tests do not verify the formatNumber functionality change. Should I add a test for that?

@park9140
Copy link
Author

@caitp, any idea when we might be able to finish this? We are releasing our project to the public over the next month and this is very important to our international customers. I would rather I didn't have to create a hack for this and just have it in angular.

@caitp
Copy link
Contributor

caitp commented May 16, 2014

@park9140 there are things I don't really like about this, particularly moving the locale reading into formatNumber, when that should be the caller's responsibility.

I think we can get this in either today or next week if we can change that.

@IgorMinar thoughts?

@park9140
Copy link
Author

@caitp, Just incase you missed it I replied to your previous comment on the code.

Basically the local provides the pattern object on which the formatNumber function relies. This is provided via the different number filters, to the formatNumber function as necessary. Internally the formatNumber function provides defaults for the pattern.

I don't think there is any way to get away from having a pattern object by which to format the numbers. The only defaults that are provided are those injected by the pattern provided by the locale.

I have another pull request around this pattern defaulting to the locale. #5674
However, I'm thinking this is not quite the way to fix it given what should really be possible is overriding the entire set of 'pattern' defaults rather than pushing individual overrides.

@caitp
Copy link
Contributor

caitp commented May 16, 2014

I understand that, however there are plenty of properties to pick from in the locale files, and some are pertinent to local representations of numbers for math, you might have different ones for currency, and so forth. So I don't think it's correct to add that logic to formatNumber().

Why not simply have the caller decide which property to use, and default to 2 if there is no locale object in the first place?

@@ -46,7 +46,7 @@ function currencyFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
return function(amount, currencySymbol){
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp, this is the primary fix here. The formatNumber function below already supported doing the formatting precision based on the pattern already passed here. So, I removed the obviously accidental force to the 2 decimal precision.

@park9140
Copy link
Author

Any help I can provide to get this bug resolved?

@park9140 park9140 closed this May 21, 2014
@park9140 park9140 reopened this May 21, 2014
@park9140
Copy link
Author

park9140 commented Jun 2, 2014

@caitp, any movement on this yet? I'm going to be replacing the currency filter entirely in our application because this hasn't been pulled.

@@ -122,7 +121,7 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
var hasExponent = false;
if (numStr.indexOf('e') !== -1) {
var match = numStr.match(/([\d\.]+)e(-?)(\d+)/);
if (match && match[2] == '-' && match[3] > fractionSize + 1) {
if (match && match[2] == '-' && match[3] > (fractionSize || pattern.maxFrac) + 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match[2] can just test if it's truthy, since there's only one possible value other than the empty string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, if fractionSize isn't defined, and pattern.maxFrac isn't defined, then it's undefined + 1 (NaN) --- you don't want that --- really, treat fractionSize as mandatory and make the number filter set it to whatever it wants to instead. Or else default to 2 instead if neither are specified

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp, the existing code structure here it is appropriate that this become NaN. This forces it to false when a fraction size is not set. Forcing this to a defined default would change the formatting significantly since currently in the undefined state of the pattern.maxFrac and fractionSize it defaults to showing the full precision of the number.

@genuinefafa
Copy link

There are many ways to fix this. I made a PR on #7582 to solve the same issue. I did accept the Angular Regulations and everything else. But nothing was pushed, yet!

@petebacondarwin petebacondarwin self-assigned this Jul 25, 2014
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Jul 27, 2014
@petebacondarwin
Copy link
Contributor

I think this is all valid stuff, as is #5674. I have reworked the commits (and tweaked the tests) to make it more clear what is actually happening here. See #8363

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

Successfully merging this pull request may close these issues.

6 participants