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

Decimal local separator #452

Closed
Romaric5M opened this issue Jul 23, 2015 · 7 comments
Closed

Decimal local separator #452

Romaric5M opened this issue Jul 23, 2015 · 7 comments
Assignees
Labels

Comments

@Romaric5M
Copy link

Hi,

As french people, I use a comma instead of point as decimal separator ...
When I use a XLFormRowDescriptorTypeDecimal row, it displays a decimal keyboard with comma which is great for amount fields.

But as I noticed the -[NSString doubleValue] isn't respecting the locale decimal separator; it expects a point, not a comma.
So finally my value isn't correct.

Is there a trick to parse correctly my amount value ?

Thanks a lot.

@Romaric5M
Copy link
Author

I find a solution by replacing

self.rowDescriptor.value =  @([self.textField.text doubleValue]);

with the following lines

NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]];
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
self.rowDescriptor.value =  [myNumFormatter numberFromString:self.textField.text];

It's not the perfect solution but it finally get the correct double value.
Hope it will be helpful for next bugfix :)

@CallMeSH
Copy link

Same problem for me. The bug fix is simple but need to be merged in pod so everyone can use it without forking the project.

@jeanmartin
Copy link
Contributor

+1

@mats-claassen
Copy link
Member

NSFormatter support was merged.

@jeanmartin
Copy link
Contributor

This is not solved. Other separators than '.' are not respected.
The line now reads:

self.rowDescriptor.value =  [NSDecimalNumber decimalNumberWithString: self.textField.text];

To respect the current locale it should read (according to this):

self.rowDescriptor.value =  [NSDecimalNumber decimalNumberWithString:self.textField.text locale:NSLocale.currentLocale];

I will create a pull request... no. 31 as of now :|

jeanmartin added a commit to jeanmartin/XLForm that referenced this issue Jun 27, 2016
mats-claassen added a commit that referenced this issue Jun 28, 2016
[fixes #452] respect the currentLocale when parsing decimal values
@ringsheep
Copy link

ringsheep commented Sep 27, 2016

This fix does not help fully. If I have set value to, e.g., "12,34" it is converting to "12.34" after I get out of this field. Using NSNumberFormatter helps to avoid this transformation, however It can't help me with changing this value to dot-format after I select this field again. With useValueFormatterDuringInput flag I am not able to insert new commas while editing, so the only way is create a custom formatter for this case.
Is it a good idea to implement a default NSNumberFormatter inside decimal row?

@ringsheep
Copy link

ringsheep commented Sep 27, 2016

The smoothest solution is to use 2 number formatters - one in form row and one as extension for NSNumber, which helps to call displayText method with formatted value, avoiding glitches of NSNumberFormatter with useValueFormatterDuringInput flag

quantityRow.valueFormatter = decimalFormatter

extension NSNumber {
    public override func displayText() -> String! {
        let formatter = NSNumberFormatter()
        formatter.decimalSeparator = ","
        formatter.locale = NSLocale.currentLocale()
        formatter.numberStyle = .DecimalStyle
        return formatter.stringFromNumber(self)
    }
}

markrickert added a commit to markrickert/XLForm that referenced this issue Sep 29, 2016
* master: (27 commits)
  Add property in XLFormTextViewCell to limit number of characters
  Add property in XLFormTextFieldCell to limit number of characters
  change XLFormUnspecifiedHeight constant to not collide with Automatic dimension
  datePicker.locale property public to developer,for example, _birthdayRow = [XLFormRowDescriptor formRowDescriptorWithTag:kBirthdayTag rowType:XLFormRowDescriptorTypeDateInline title:@"Birthday"]; [_birthdayRow.cellConfig setObject:[[NSLocale alloc] initWithLocaleIdentifier:@"en"] forKey:@"locale"];
  Refactored '-(id)init' to '- (instancetype)init'
  Update 'initializeForm' example.
  Update pod badge
  [fixes xmartlabs#452] respect the currentLocale when parsing decimal values
  Remove the fix for suppressing "Empty snapshot" warnings when presenting action sheets
  Updated code to actually use the optionTitle variable
  Fixed selector action sheet to use the row's value transformer
  update pod version
  added count to empty check. Helpful to validate multiple selectors being empty
  Fix decimal number formatting. closes xmartlabs#514
  Added height property to XLFormRowDescriptor
  updated readme with some comments. Fixes xmartlabs#771, fixes xmartlabs#727, fixes xmartlabs#696
  Add support for NSFormatter
  Do validation can be nullable. fixes xmartlabs#705
  fix crash when using cell style Value2. closes xmartlabs#770
  Fix when the textFieldPercentage is applied. Closes xmartlabs#776
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants